加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 06:10:46 所属栏目:百科 来源:网络整理
导读:我看着Xcode 7.3笔记,我注意到这个问题。 The ++ and — operators have been deprecated 有人能解释为什么它被废弃?我是对的,在新版本的Xcode现在你要使用,而不是这个x = 1; 例: for var index = 0; index 3; index += 1 { print("index is (index)")
我看着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?”

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读