I'm following a raywenderlich.com tutorial on using the Google Maps
iOS SDK. I came across this piece of code which is halfway down this
link here: https://www.raywenderlich.com/109888/google-maps-ios-sdk-tutorial.
I am familiar with Swift but I do not get what the piece of code after geocoder.reverseGeocodeCoordinate(coordinate) does; specifically, how can you just place the curly brackets right after the method call and what does it accomplish? I am asking this in terms of Swift syntax.
I am familiar with Swift but I do not get what the piece of code after geocoder.reverseGeocodeCoordinate(coordinate) does; specifically, how can you just place the curly brackets right after the method call and what does it accomplish? I am asking this in terms of Swift syntax.
func reverseGeocodeCoordinate(coordinate: CLLocationCoordinate2D) {
// 1
let geocoder = GMSGeocoder()
// 2
geocoder.reverseGeocodeCoordinate(coordinate) { response, error in
if let address = response?.firstResult() {
// 3
let lines = address.lines as! [String]
self.addressLabel.text = lines.joinWithSeparator("\n")
// 4
UIView.animateWithDuration(0.25) {
self.view.layoutIfNeeded()
}
}
}
}
-----Answer-----------
1.
This is called a "trailing closure" or "trailing closure syntax". It's described in Apple's docs here:
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html#//apple_ref/doc/uid/TP40014097-CH11-ID102
In summary, if the last parameter of a method or function is a closure, you can provide it immediately after the closing
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html#//apple_ref/doc/uid/TP40014097-CH11-ID102
In summary, if the last parameter of a method or function is a closure, you can provide it immediately after the closing
)
of the function call and the previous arguments. Example:func sendMessage(text:String, withCallback:(Bool)->()) {
// Implementation
}
let message = "hello"
sendMessage(message) {
let result = $0 ? "Suceeded" : "Failed"
print(result)
}
If a function take only one parameter and that parameter is a closure, then you don't need to use ()
at all and can just pass the closure immediately after the function name. For example see how this filter method is called:let boolArray = [true, true, false, true, false]
let filtered = boolArray.filter { $0 == false } // Only the falses
2.
It is called a trailing closure. A trailing closure is a closure expression that is written outside of the parentheses of the function call.
The only requirement here is that the closure must be function's final argument.
Given the following function, these two calls are identical:
The only requirement here is that the closure must be function's final argument.
Given the following function, these two calls are identical:
func someAPICall(url: String, completion: (Bool -> Void)) {
// make some HTTP request
completion(result.isSuccess)
}
someAPICall("http://httpbin.org/get") { success in
print("Success", success)
}
someAPICall("http://httpbin.org/get", completion: { success in
print("Success", success)
})
0 nhận xét:
Post a Comment