2016-10-18 6 views
0

私はthisを読んでいますが、私には分かりません。しかし、私はこの閉包の構文を理解しています:後閉じはどのように機能しますか?

var cal = {(num1: Int)-> Int in 
    return num1 * 2; 
} 

var clusers = [cal, 
       {(num1:Int) -> Int in return num1 * 3}, 
       {(num1:Int) -> Int in num1 * 4}, 
       {(num1:Int) in num1 * 5}, 
       { num1 in num1 * 6}, 
       { $0 * 7}] 
for cluser in clusers{ 
    cluser(100) 
} 

後閉じを行うにはどうしたらいいですか?

基本的に、後ろに閉じた部分があります。私はそれを理解することができません:

//call dispatch async to send a closure to download queue 
    dispatch_async(download) {() ->Void in 

    //some code goes here   
    } 
+1

このビデオを確認するhttps://videos.raywenderlich.com/courses/intermediate-swift-3/lessons/2詳細は他のリンクhttps://www.natashatherobot.com/swift-trailing-closure-syntax /およびhttps://www.quora.com/Apple-Swift-programming-language-What-is-a-trailing-closure – AK1

+0

これはまた、本当に良いリソースです。https://www.weheartswift.com/closures/ – sbarow

+0

@ AK1ありがとう私はそれを –

答えて

1

は、あなたが上記の関数にクロージャを渡す必要がある場合は、

someFunctionThatTakesAClosure(5, closure: { 
    // code included in closure 
}) 

以下のようなものを書く

func someFunctionThatTakesAClosure(index: Int, closure:() -> Void) { 

} 

これは私の関数でありましょう。しかし上記の関数のように、関数に渡される最後の引数がクロージャであれば、このようなコードを書くこともできます。

someFunctionThatTakesAClosure(5) { 
    // code included in closure 
} 

これが末尾クロージャと呼ばれる理由です。

+0

私は訂正しました。 –

+0

@ハミッシュありがとう、指摘してくれてありがとう。 –

関連する問題