俺、サービス売って家買うんだ

Swift, Kotlin, Vue.js, 統計, GCP / このペースで作ってればいつか2-3億で売れるのがポっと出来るんじゃなかろうか

【Swift】関数にオプショナルのブロックを渡す

f:id:ie-kau:20151120074851p:plain:w400


括弧が多くて意味不明になったので一旦メモ。

func execute(completion: (() -> Void)?) {
    if let unwraped = completion {
        unwraped()
    }
}

execute { 
    print("hoge") // hoge
}

これだとコンパイルエラー

func execute(completion: () -> Void?) {
    if let unwraped = completion {
        unwraped()
    }
}

error: initializer for conditional binding must have Optional type, not ‘() -> Void?’ if let unwraped = completion {