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

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

问题描述

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

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

++和-运算符已弃用

The ++ and -- operators have been deprecated

有人可以解释为什么不推荐使用它吗?我是对的,现在在新版本的Xcode中,您将使用此x += 1代替++;

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;

示例:

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. 这是学习Swift时必须学习的另一个功能
  2. x += 1
  3. 短很多
  4. Swift不是C.不应为了取悦C程序员而将它们带走
  5. 它的主要用途是C样式的循环:for i = 0; i < n; i++ { ... },Swift具有更好的替代方式,例如for i in 0..<n { ... }(C样式的循环是
  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. 这些运算符增加了学习Swift作为第一门编程语言的负担-或在其他情况下您还不了解其他语言的运算符.

  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.

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

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

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

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具有强大的功能,消除了许多您在其他语言中以C风格使用++ i进行循环的常见原因,因此在编写良好的Swift代码中相对很少使用这些原因.这些功能包括for-in循环,范围,枚举,映射等.

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.

尽管Swift具有明确定义的求值顺序,但是依赖于它的任何代码(例如foo(++ a,a ++))也将是不受欢迎的,即使它定义得很好.

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.

最后,这些不符合如果我们还没有这些,我们可以将它们添加到Swift 3吗?"的度量标准.

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天全站免登陆