1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Swift】可変長引数の関数に配列を渡したい!ときのworkaround

Posted at

結論

自前の関数なら同名の関数をoverloadできます。

 // 配列を引数に取る関数側にロジックを実装する
 func greet(_ people: [String]) {
     print("Hello, \(people.joined(separator: ", "))!")
 }
 
 // 可変長引数を持つ関数側では、内部で配列を引数に取る関数を呼び出す
 func greet(_ people: String...) {
     greet(people)
 }

言語仕様として、引数に渡すときに配列を展開することはできないので、フレームワーク内の可変長引数(例えば、printなど)の関数に配列を渡すのは現状は難しいようです。

参考

Passing an array to a function with variable number of args in Swift

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?