swift – 和操作符已被弃用Xcode 7.3

前端之家收集整理的这篇文章主要介绍了swift – 和操作符已被弃用Xcode 7.3前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我看着Xcode 7.3笔记,我注意到这个问题。

The ++ and — operators have been deprecated

有人能解释为什么它被废弃?我是对的,在新版本的Xcode现在你要使用,而不是这个x = 1;

例:

for var index = 0; index < 3; index += 1 {
    print("index is \(index)")
}

来自Chris Lattner的 full explanation here,Swift的创作者。我将总结几点:

>这是另一个功能,你必须学习,而学习Swift
>不比x = 1短得多
> Swift不是C.不应该携带他们只是为了请C程序员
>它主要用于C型for循环:for i = 0; i< n; i {...},其中Swift有更好的替代方案,例如对于i in 0 ..< n {...}(C风格for循环是going out as well)
>可能难以阅读和维护,例如,x – x或foo(x,x)的值是什么?
>克里斯·拉特纳不喜欢它。

对于那些感兴趣的人(为了避免链接腐烂),Lattner用他自己的话说的原因是:

  1. These operators increase the burden to learn Swift as a first programming language – or any other case where you don’t already know these operators from a different language.

  2. Their expressive advantage is minimal – x++ is not much shorter than x += 1.

  3. Swift already deviates from C in that the =,+= and other assignment-like operations returns Void (for a number of reasons). These operators are inconsistent with that model.

  4. Swift has powerful features that eliminate many of the common reasons you’d use ++i in a C-style for loop in other languages,so these are relatively infrequently used in well-written Swift code. These features include the for-in loop,ranges,enumerate,map,etc.

  5. Code that actually uses the result value of these operators is often confusing and subtle to a reader/maintainer of code. They encourage “overly tricky” code which may be cute,but difficult to understand.

  6. While Swift has well defined order of evaluation,any code that depended on it (like foo(++a,a++)) would be undesirable even if it was well-defined.

  7. These operators are applicable to relatively few types: integer and floating point scalars,and iterator-like concepts. They do not apply to complex numbers,matrices,etc.

Finally,these fail the metric of “if we didn’t already have these,would we add them to Swift 3?”

猜你在找的Swift相关文章