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

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

问题描述

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

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


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

有什么想法修复此警告吗?将来不推荐使用 ++ - 的任何理由?

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

推荐答案

自Swift 2.2起,你应该使用 + = 1 - = 1 而不是。

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

在查看Swift的演变之后,有一些理由要删除这些运算符:

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



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

请查看迅速演变了解更多信息。

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

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