【Swift】関数にオプショナルのブロックを渡す
括弧が多くて意味不明になったので一旦メモ。
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 {