IOS8 Trailing Closures in Swift

let callActionHandler = { (action:UIAlertAction!) -> Void) in
        let alertMessage = UIAlertController(title: "Service Unavailable", message: "Sorry, the call feature is not available yet. Please retry later", preferredStyle: UIAlertControllerStyle.Alert)
        alertMessage.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
        self.presentViewController(alertMessage, animated: true, completion: nil)
    };    

// Code Snippet 1
let callAction = UIAlertAction(title: "Call" + "123-000-\(indexPath.row)", style: UIAlertActionStyle.Default ) { (action:UIAlertAction!) -> Void in
        println("check this out")
}

// Code Snippet 2
let callAction = UIAlertAction(title: "Call" + "123-000-\(indexPath.row)", style: UIAlertActionStyle.Default, handler: { (action:UIAlertAction!) -> Void in  
        println("Lets check this out")
})

// Code Snippet 3
let callAction = UIAlertAction(title: "Call" + "123-000-\(indexPath.row)", style: UIAlertActionStyle.Default , handler: callActionHandler)
  • Here we have 3 code snippets, my doubts are:
    • What is the difference between Code Snippet 1 and Code Snippet 2?
    • Which of fragment 1 or fragment 2 is the best representation and should be used?
    • What exactly does Snippet 1 code mean? Is this a kind of property (completion) of observation in fast?
    • The way we want to write iOS8 is shown in Snippet 1, i.e. when I press the enter button during Xcode autocompletion, it converts to Snippet 1. Should we use Code Snippet 1 or still prefer to use Snippet 2/3, because they are easy to understand?

thank

+4
source share
1 answer

You asked:

  • What is the difference between Code Snippet 1 and Code Snippet 2?

1 "-" 2. . "-" : , :

, , . - , ( ) ,

, 1 2 .

  1. 1 2 ?

, 1, . , .

  1. Snippet 1 ? () ?

, , 2.

  1. , iOS8, Snippet 1, .. Xcode, Snippet 1. Code Snippet 1 - Snippet 2/3, ?

, , , . , , .


, , , :

let callAction = UIAlertAction(title: "Call 123-000-\(indexPath.row)", style: .default) { action in
    print("check this out")
}
+4

All Articles