“++"和“--"运算符已被弃用 Xcode 7.3 [英] The "++" and "--" operators have been deprecated Xcode 7.3

查看:19
本文介绍了“++"和“--"运算符已被弃用 Xcode 7.3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看 Xcode 7.3 注释并注意到这个问题.

<块引用>

++ 和 -- 运算符已被弃用

有人可以解释为什么不推荐使用它吗?我是否正确,在新版本的 Xcode 中,您现在要使用 ++ 而不是这个 x += 1;

示例:

for var index = 0;指数<3;指数 += 1 {打印(索引是(索引)")}

解决方案

A 这里有完整的解释,来自 Swift 的创造者 Chris Lattner.我将总结几点:

  1. 这是你在学习 Swift 时必须学习的另一个功能
  2. 不比x += 1
  3. 短多少
  4. Swift 不是 C.不应该仅仅为了取悦 C 程序员而将它们带过来
  5. 它的主要用途是在 C 风格的 for 循环中: for i = 0;我<n;i++ { ... },Swift 有更好的选择,比如 for i in 0..<n { ... } (C 风格的 for 循环是 也出去)
  6. 阅读和维护可能很棘手,例如,x - ++xfoo(++x, x++) 的值是什么?
  7. 克里斯·拉特纳不喜欢它.


对于那些感兴趣的人(并避免链接失效),用他自己的话来说,Lattner 的理由是:

<块引用>

  1. 这些运算符增加了学习 Swift 作为第一编程语言的负担 - 或者在任何其他情况下,您还没有从其他语言中了解这些运算符.

  2. 它们的表达优势很小 - x++ 并不比 x += 1 短多少.

  3. Swift 已经与 C 有所不同,因为 =、+= 和其他类似赋值的操作返回 Void(出于多种原因).这些运算符与该模型不一致.

  4. Swift 具有强大的功能,可以消除您在其他语言的 C 样式 for 循环中使用 ++i 的许多常见原因,因此这些功能在编写良好的 Swift 代码中相对较少使用.这些功能包括 for-in 循环、范围、枚举、映射等.

  5. 实际使用这些运算符的结果值的代码对于代码的阅读者/维护者来说通常是令人困惑和微妙的.他们鼓励过于棘手"代码可能很可爱,但很难理解.

  6. 虽然 Swift 有明确定义的求值顺序,但任何依赖于它的代码(如 foo(++a, a++))即使定义明确也是不可取的.

  7. 这些运算符适用于相对较少的类型:整数和浮点标量,以及类似迭代器的概念.它们不适用于复数、矩阵等.

最后,这些没有达到如果我们还没有这些,我们会将它们添加到 Swift 3 中吗?"的度量标准?

I am looking at Xcode 7.3 notes and I notice this issue.

The ++ and -- operators have been deprecated

Could some one explain why it is deprecated? And am I right that in new version of Xcode now you going to use instead of ++ this x += 1;

Example:

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

解决方案

A full explanation here from Chris Lattner, Swift's creator. I'll summarize the points:

  1. It's another function you have to learn while learning Swift
  2. Not much shorter than x += 1
  3. Swift is not C. Shouldn't carry them over just to please C programmers
  4. Its main use is in C-style for loop: for i = 0; i < n; i++ { ... }, which Swift has better alternatives, like for i in 0..<n { ... } (C-style for loop is going out as well)
  5. Can be tricky to read and maintain, for eg, what's the value of x - ++x or foo(++x, x++)?
  6. Chris Lattner doesn't like it.


For those interested (and to avoid link rot), Lattner's reasons in his own words are:

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

这篇关于“++"和“--"运算符已被弃用 Xcode 7.3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆