微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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?”

原文地址:https://www.jb51.cc/swift/321382.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐