更新到Swift3.0后有些差异是需要我们重新了解的。
Swift 3改变可以分为两个大的部分
- 移除在Swift 2.2中弃用的方法
- 语言更加安全,优雅
1、Range
Range在Swift3.0的初始化方式有所不同
var range:Range<Int> = Range(uncheckedBounds: (0,5))
//结果通[0..<5]一致
Range初始化方法需要我们传入一个元祖,一个最小值和一个最大值
public init(uncheckedBounds bounds: (lower: Bound,upper: Bound))
2、ClosedRange
var range:ClosedRange<Int> = ClosedRange(uncheckedBounds: (0,5))
//结果通[0...5]一致
3、replaceSubrange
//闭区间
//method1
var array2 = [2,1,2]
array2.replaceSubrange(ClosedRange(uncheckedBounds:(0,1)),with: [1])
//method2
array2.replaceSubrange(CountableClosedRange(uncheckedBounds:(0,with: [1])
//开区间
//method3
array2.replaceSubrange(Range(uncheckedBounds:(0,2)),with: [1])
//method4
array2.replaceSubrange(CountableRange(uncheckedBounds:(0,with: [1])
通过对比了解到method1和method2结果一致,method3和method4结果一致。
4、inout
//数据交换
func swap( a:inout Int,b:inout Int){
let c = a
a = b
b = c
}
var x = 2,y = 3
swap(&x,&y)
5、indices
//创建所有能够用来访问字符串中独立字符的索引范围 Range
for index in greeting.characters.indices{
print("\(greeting[index]) ",terminator: "")
}
6 、do while 改为 repeat while
repeat {
statements
} while condition