LoginSignup
2
2

More than 5 years have passed since last update.

Shorthand External Parameter Names in Swift

Posted at

Swiftでは名前付きパラメタが使える。

func sayHello(name name: String, age age: Int) -> String {
    return "Hello, \(name). Are you \(age) years old?"
}

sayHello(name: "Test", age: 14)

でも基本的に外から見えるパラメタの名前と関数内で使う変数名は同じ場合が多いような気がするので
そういう場合は以下のように簡単に書くこともできる。

func sayHello(#name: String, #age: Int) -> String {
    return "Hello, \(name). Are you \(age) years old?"
}

sayHello(name: "Test", age: 14)

自動的にnameとageという名前付きパラメタを作ってくれる

2
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
2
2