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

swift – 和 *** 作符已被弃用Xcode 7.3,第1张

概述我看着Xcode 7.3笔记,我注意到这个问题。 The ++ and — operators have been deprecated 有人能解释为什么它被废弃?我是对的,在新版本的Xcode现在你要使用,而不是这个x = 1; 例: for var index = 0; index < 3; index += 1 { print("index is \(index)")} 来自Chr 我看着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用他自己的话说的原因是:

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.

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

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.

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.

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.

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.

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 – 和 *** 作符已被弃用Xcode 7.3全部内容,希望文章能够帮你解决swift – 和 *** 作符已被弃用Xcode 7.3所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1060328.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-25
下一篇2022-05-25

发表评论

登录后才能评论

评论列表(0条)

    保存