'++' 已弃用:它将在 Swift 3 中删除 [英] '++' is deprecated: it will be removed in Swift 3

查看:26
本文介绍了'++' 已弃用:它将在 Swift 3 中删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新到 Xcode 7.3 后,我的项目中显示了一堆警告.

<块引用>

'++' 已弃用:它将在 Swift 3

中删除

有没有办法解决这个警告?++-- 将来会被弃用的任何原因?

解决方案

从 Swift 2.2 开始,您应该使用 += 1-= 1 代替.

在查阅 Swift 的演变之后,有一些原因需要删除这些运算符:

<块引用>

  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 中吗?"的度量标准?

请查看快速进化了解更多信息.

After update to Xcode 7.3, there are bunch of warnings showing in my project.

'++' is deprecated: it will be removed in Swift 3

Any idea to fix this warning ? Any reasons why the ++ and -- will be deprecated in the future ?

解决方案

Since Swift 2.2, you should use += 1 or -= 1 instead.

And after looking up Swift's evolution, there are some reasons for removing these operators:

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

Please check out Swift evolution for more info.

这篇关于'++' 已弃用:它将在 Swift 3 中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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