博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS-Swift中的递增(++)和递减(--)被取消的原因
阅读量:4565 次
发布时间:2019-06-08

本文共 1943 字,大约阅读时间需要 6 分钟。

所以在目前Swift5的版本中,只能用+=1和-=1来进行递增和递减了

如果坚持用++或--将会提示以下错误:

Use of unresolved operator '++'; did you mean '+= 1'?

Use of unresolved operator '--'; did you mean '-= 1'?
也是好奇为毛Swift把这么好用的语法取消了,之前也有老外咨询过Chris Lattner(Swift语言缔造者),并获得了回复。 最近也是被学生问起这个问题,所以拿出来分享一下,以下是我总结的:

这是Swift的新功能(我就这么弄,怎么了)

++也并没比 += 1简洁多少
别把Swift和别的语言混淆在一起(这,就是个性)
别的语言的++主要是用在for循环中:for i = 0; i < n; i++ { ... },但Swift语言的for循环可以这么写:for i in 0..<n { ... },所以++就没什么用了
++读起来不太拟人,感觉没什么意义,不符合Swift语言一贯的超长命名方式,比如x - ++x 或 foo(++x, x++),没注释的话之后连自己都看不懂。
作者本人不喜欢++和--
一句话:我们不一样!
以下是Chris Lattner答复的原文:
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?"

转载于:https://www.cnblogs.com/hyhy904/p/11153900.html

你可能感兴趣的文章
新秀系列C/C++经典问题(四)
查看>>
memset函数具体说明
查看>>
经常使用的android弹出对话框
查看>>
确保新站自身站点设计的合理性的六大注意点
查看>>
promise
查看>>
Go 网络编程笔记
查看>>
[]Java面试题123道
查看>>
中间件与auth认证的那点儿所以然
查看>>
Scala
查看>>
Android 中LinearLayout控件属性
查看>>
面向对象之多态性
查看>>
树状数组
查看>>
【2019.8.14 慈溪模拟赛 T1】我不是!我没有!别瞎说啊!(notme)(BFS+DP)
查看>>
多任务--进程 及 进程间通信
查看>>
多线程/多进程+QProgressBar实现进度条
查看>>
多任务(进程)案例----- 拷贝文件夹
查看>>
Kotlin的快速入门
查看>>
底层原理
查看>>
21. Merge Two Sorted Lists
查看>>
shiro设置加密算法源码解析
查看>>