众所周知,在很多编程语言中,对一个变量递增1用++,递减1用--,在Swift3之前也是可以这么用的,但之后被取消了。

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

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

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

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

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

  1. 这是Swift的新功能(我就这么弄,怎么了)
  2. ++也并没比 += 1简洁多少
  3. 别把Swift和别的语言混淆在一起(这,就是个性)
  4. 别的语言的++主要是用在for循环中:for i = 0; i < n; i++ { ... },但Swift语言的for循环可以这么写:for i in 0..<n { ... },所以++就没什么用了
  5. ++读起来不太拟人,感觉没什么意义,不符合Swift语言一贯的超长命名方式,比如x - ++xfoo(++x, x++),没注释的话之后连自己都看不懂。
  6. 作者本人不喜欢++--

一句话:我们不一样!

以下是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?"

广告时间:小弟的iOS12零基础视频教程(每章皆可试听):

m.study.163.com/provider/48…

iOS-Swift中的递增(++)和递减(--)被取消的原因-官方答复相关推荐

  1. java中自动递增和递减

    在java中为了操作方便提供了"自动递增"和"自动递减运算",常称为"自增"和"自减":递加操作符是"++&q ...

  2. java中关于递增和递减两种方式的差别

    一.什么是递增和递减? 递增操作符:"++" 理解意思:每次增加一个单位. 递减操作符:"--" 理解意思:每次减少一个单位. 1.前缀式递增和递减(++a或- ...

  3. 为什么要避免使用JavaScript中的递增(“++”)和递减(“ - ”)运算符?

    本文翻译自:Why avoid increment ("++") and decrement ("--") operators in JavaScript? O ...

  4. java递减_关于Java中递增和递减运算符的有趣事实

    关于Java中的递增和递减运算符,有许多有趣的事实.我们将通过示例讨论其中的一些-增量和减量运算符不能与'final'变量一起使用.这是由于与'final'关键字相关联的变量无法更改的事实- 示例pu ...

  5. iOS Swift 5中的键盘处理

    This article was originally published at CometChat. 本文最初在CometChat上发布. "Handling Keyboard in iO ...

  6. matlab分数递减指令,5.2.3 PLC的递增、递减指令

    递增.递减指令 递增.递减指令用于对输入无符号数字节.符号数字.符号数双字进行加1或减1的操作.指令格式如表5-19所示. 1. 递增字节(INC-B)/递减字节(DEC-B)指令 递增字节和递减字节 ...

  7. 使用 Swift 在 iOS 10 中集成 Siri —— SiriKit 教程

    使用 Swift 在 iOS 10 中集成 Siri -- SiriKit 教程 转载地址:http://swift.gg/2016/06/28/adding-siri-to-ios-10-apps- ...

  8. extjs chart无法在panel中显示_HighChart教程:Swift中的Highcharts iOS库

    以下文章总结了如何在Swift中使用Highcharts iOS库. 我们将对2016/2017赛季四支顶级冠军联赛球队的统计数据进行可视化. A.如何创建Highcharts项目 入门 在Xcode ...

  9. c语言中递增递减运算符,递增++和递减-C ++中的运算符

    增量运算符++对其操作数加1,而减量运算符-从其操作数减1.所以,x = x+1; is the same as x++; 同样,x = x-1; is the same as x--; 递增和递减运 ...

最新文章

  1. java模拟网银登录_用java编写模拟网上银行登录及存取款业务
  2. Javascript 绑定事件和 this理解
  3. Android——apk反编译
  4. MapReduce数据连接
  5. 016_Spring中电子邮件
  6. 网页侧边浮动条的实现
  7. MWeb 的基本使用
  8. Android内存泄漏总结
  9. hihocoder 1035 : 自驾旅行 III
  10. 一片关于Bootstarp4的文章
  11. 2018.9.19作业
  12. linux一些基本知识
  13. 线元法输入曲线要素_Origin入门教程(三):Origin中曲线怎么平移?
  14. ps无法加载扩展未经正确签署解决方法
  15. SuperMap IClient for Leaflet 加载高斯克吕格投影三度分带CGCS2000大地坐标系WMTS服务
  16. VARCHART XGantt 甘特图中的日期和夏令时
  17. CDliux--minidwep 无线密码渗透测试
  18. selenium怎样定位web动态提示语
  19. USB之Cyusb3014芯片原理之四控制传输实现原理(十二) 2022-04-01
  20. C语言字符串使用指南

热门文章

  1. 朴素、Select、Poll和Epoll网络编程模型实现和分析——Poll模型
  2. Windows上VS2017单步调试FFmpeg源码的方法
  3. 《GPU高性能编程CUDA实战》中代码整理
  4. 【Qt】enum和QString的相互
  5. linux 查看剪贴板历史,Clipboard History Manager插件,查看浏览器剪贴板历史记录
  6. avue validate 变为不可编辑_排版技巧——如何用 Word 编辑参考文献
  7. linux c时间戳转换,Android C/C++代码中将时间戳转换为标准时间
  8. linux命令安装组件,Linux安装各种组件
  9. nodejs mysql 异步_Gearman + Nodejs + MySQL UDF异步实现 MySQL 到 Redis 的数据同步
  10. Java多线程复习:2(线程的创建和使用)