Swift中的部分更新与旧版的区别

时间:2025-04-03 23:37:02

1.

函数中的外部变量名取消 “#”方式,仅能用直接命名方式

 错误
func swift(#str :NSString){}
正确
func swift(str str :NSString){}

2.

UIButton 的声明区别

错误:
let button = UIButton.buttonWithType(UIButton.Custom) as UIButton
正确:
let button = UIButton(type: UIButtonType.System)

3.

Printable 协议更名为  CustomStringConvertible

4.

经典的do-while语句改名了,改为了repeat-while:

 var i = 
repeat {
    i++
    print(i)
} while i < 

 

5.

在Swift1中,有'println()'和'print()'两个在控制台打印语句的方法,前者是换行打印,后者是连行打印。在Swift2中,'println()'已成为过去,取而代之的是他俩的结合体。

print("我要换行!")