LoginSignup
1
1

More than 5 years have passed since last update.

Swift のクロージャについてのメモ

Last updated at Posted at 2015-06-22

検証環境: 1.2, 2.0 (Xcode7.0 beta)

とあるコードを読んでてとっさに理解できなかったのでメモ。

初期化が期待されるプロパティを持つ struct の初期化

struct A {
    let str: String
}

var a = A("a") // error: missing argument label 'str:' in call var a = A("a")

プロパティを暗黙のイニシャライザで初期化しようとすると、当然ラベルがいる。

関数による初期化が期待されるプロパティを持つ struct の初期化

struct B {
    let someFunc: String -> String
}

var b = B { x in "result" }
b.str("b") // "result"

ラベルがいらないんだなあ。

Trailing closures はラベルを省略できる

ドキュメント に普通に書いてあった。
いきなり Struct の初期化時に使われて一瞬何やってるか分からなかったという話でした。

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