-2
func gradientOfView(withColours: UIColor..., locations: [NSNumber])
迅速變PARAM與陣列
func gradientOfView(withColours: [UIColor], locations: [NSNumber])
哪家比較好?
什麼時候使用變量參數替換數組作爲參數?
func gradientOfView(withColours: UIColor..., locations: [NSNumber])
迅速變PARAM與陣列
func gradientOfView(withColours: [UIColor], locations: [NSNumber])
哪家比較好?
什麼時候使用變量參數替換數組作爲參數?
可變參數函數接受0或多個參數,見下段
func f0(items: Any...) {
print(type(of: items))
items.forEach { (i) in
print("\t",i)
}
}
func f1(items: [Any]) {
print(type(of: items))
items.forEach { (i) in
print("\t",i)
}
}
f0(items: 1,2)
f1(items: [1,2])
f0(items: [1,2])
f1(items: [1,2])
f0()
它打印
Array<Any>
1
2
Array<Any>
1
2
Array<Any>
[1, 2]
Array<Any>
1
2
Array<Any>
聲明
f1()
將在編譯 產生一個錯誤。如果你不沒有很好的理由用可變參數來聲明函數參數,避免它。