2017-05-24 3 views
-2

func gradientOfView(withColours: UIColor..., locations: [NSNumber])迅速な変数paramが優れている、配列

func gradientOfView(withColours: [UIColor], locations: [NSNumber])

と比較しますか?

可変パラメータを使用する場合は、配列をparamとして置き換えますか?

答えて

0

可変引数機能あなたドン場合

f1() 

は、コンパイル にエラーが発生します

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> 

を印刷し、文を0以上のパラメータを受け入れますバリデーショナルで関数を宣言するのは非常に良い理由がありますパラメータ、それを避ける。

関連する問題