1
3

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 5 years have passed since last update.

Swift メソッド

Posted at

メソッド

  • 型に紐づけられた関数
  • 型のインスタンスの振る舞いを実現するた目に使用される

定義方法

型の定義の内部でfuncキーワードを使用する。

func メソッド名(引数) -> 戻り値の型 {
    メソッド呼び出し時に実行される文
}

メソッドを呼び出すには、型のインスタンスが代入された変数や定数に.とメソッド名と()を付けて、変数名.メソッド名()のように書く。


struct Greeting {
    func greet(user: String) -> Void {
        print("My name is \(user).")
    }
}

let greeting = Greeting()
greeting.greet(user: "KunioTerada")
My name is KunioTerada.
1
3
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
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?