0
0

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 1 year has passed since last update.

引数ラベルってなんだ...?省略ってなんだぁ?

Last updated at Posted at 2023-05-21

Swiftにおける引数の書式

【背景】

新卒研修の関係で、SwiftUIという新しめの環境ではなく、storyboardというものを勉強する為に、同期に借りたSwiftの参考書↓
たった2日でマスターできるiPhoneアプリ開発集中講座Xcode11|Swift5|対応
で、

引数ラベルの省略は、_(アンダースコア)を使う。

と書いてあった。
よく分からんが「ほーん」と思いながら進めていくと

func tableView(numberOfRowsInSection section: Int)
//                   ↑   こいつらは何だ?      ↑  

こんな記述があるじゃないか。

Swiftの関数の引数が1個につき3項目あることに気づき、
「えっ、引数って <引数名: データ型>だけじゃなかったんか..?」
と思ったから調べました。

Swiftの引数は、 (引数ラベル 引数名: データ型)

で構成されるようです。
Swiftで関数を、引数の省略なしで書くこのようになります。

func 関数名(引数ラベル 引数名: データ型)-> 返り値のデータ型{
    //~~中身が詰まってる~~
    return 〇〇
}

つまりどういうことかと言いますと

名前 意味
引数ラベル 呼び出す時の名前
引数名 関数内での名前
データ型 引数のデータ型
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?