2
1

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】クロージャ

2
Last updated at Posted at 2021-01-26

はじめに

この記事では、Swiftのクロージャについてまとめました。
⚠️当方初学のため認識違いがある可能性大です
誤りを見つけた際はコメントにて指摘していただけると幸いです🙇‍♀️🙇‍♂️

参考

この記事は、以下の記事を参考にして作成しました。

クロージャとは

クロージャとは、スコープ内の変数や定数を保持したひとまとまりの処理。
また、クロージャは関数の一種であり再利用可能。
クロージャはクロージャ式で定義し、名前が不要であったり型推論によって型の記述が省略できたり関数より比較的軽量化して定義できる。

基本的な使い方

{(引数名1: 型, 引数名2: 型...) -> 戻り値の型 in
  クロージャの実行時に実行される処理
  必要に応じてreturn文で戻り値を返却する
}

上記がクロージャ式の雛形。
処理が複数ある場合returnの記述が必要だが、処理が一つしかない場合省略できる。
引数や返り値がVoid型なら以下のように型名の記述を省略できる。

{() -> () in }
2
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?