Kevin, I thoroughly enjoyed your "Learn to code in Swift" book and I am so glad I discovered your content. In this book you mentioned use of "Closure" to process asynchronous web requests in later part of the series. Does that mean it if covered in one of your other books or is it something you are planning on releasing in another book in the future?
Do you have any pointers on using the Generic Response Object Serialization of Alamofire and what exactly the code below means in your usual elegant and simple way?
Generic Response Object Serialization
Generics can be used to provide automatic, type-safe response object serialization.
@objc public protocol ResponseObjectSerializable {
init?(response: NSHTTPURLResponse, representation: AnyObject)
}
extension Alamofire.Request {
public func responseObject<T: ResponseObjectSerializable>(completionHandler: (NSURLRequest, NSHTTPURLResponse?, T?, NSError?) -> Void) -> Self {
let serializer: Serializer = { (request, response, data) in
let JSONSerializer = Request.JSONResponseSerializer(options: .AllowFragments)
let (JSON: AnyObject?, serializationError) = JSONSerializer(request, response, data)
if response != nil && JSON != nil {
return (T(response: response!, representation: JSON!), nil)
} else {
return (nil, serializationError)
}
}
return response(serializer: serializer, completionHandler: { (request, response, object, error) in
completionHandler(request, response, object as? T, error)
})
}
}
final class User: ResponseObjectSerializable {
let username: String
let name: String
required init?(response: NSHTTPURLResponse, representation: AnyObject) {
self.username = response.URL!.lastPathComponent
self.name = representation.valueForKeyPath("name") as String
}
}
Alamofire.request(.GET, "http://example.com/users/mattt")
.responseObject { (_, _, user: User?, _) in
println(user)
}