Skip to content

Commit

Permalink
Updated the README code samples for advanced response serialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
cnoon committed Jul 24, 2015
1 parent 1978c2c commit 86b95d0
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -601,23 +601,21 @@ For example, here's how a response handler using [Ono](https://github.com/mattt/

```swift
extension Request {
class func XMLResponseSerializer() -> Serializer {
return { request, response, data in
public static func XMLResponseSerializer() -> GenericResponseSerializer<ONOXMLDocument> {
return GenericResponseSerializer { request, response, data in
if data == nil {
return (nil, nil)
}

var XMLSerializationError: NSError?
let XML = ONOXMLDocument(data: data, &XMLSerializationError)
let XML = ONOXMLDocument(data: data!, error: &XMLSerializationError)

return (XML, XMLSerializationError)
}
}

func responseXMLDocument(completionHandler: (NSURLRequest, NSHTTPURLResponse?, ONOXMLDocument?, NSError?) -> Void) -> Self {
return response(serializer: Request.XMLResponseSerializer()) { request, response, XML, error in
completionHandler(request, response, XML as? ONOXMLDocument, error)
}
public func responseXMLDocument(completionHandler: (NSURLRequest, NSHTTPURLResponse?, ONOXMLDocument?, NSError?) -> Void) -> Self {
return response(responseSerializer: Request.XMLResponseSerializer(), completionHandler: completionHandler)
}
}
```
Expand All @@ -631,11 +629,11 @@ Generics can be used to provide automatic, type-safe response object serializati
init?(response: NSHTTPURLResponse, representation: AnyObject)
}

extension Alamofire.Request {
extension 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)
let responseSerializer = GenericResponseSerializer<T> { request, response, data in
let JSONResponseSerializer = Request.JSONResponseSerializer(options: .AllowFragments)
let (JSON: AnyObject?, serializationError) = JSONResponseSerializer.serializeResponse(request, response, data)

if let response = response, JSON: AnyObject = JSON {
return (T(response: response, representation: JSON), nil)
Expand All @@ -644,9 +642,7 @@ extension Alamofire.Request {
}
}

return response(serializer: serializer) { request, response, object, error in
completionHandler(request, response, object as? T, error)
}
return response(responseSerializer: responseSerializer, completionHandler: completionHandler)
}
}
```
Expand All @@ -656,7 +652,7 @@ final class User: ResponseObjectSerializable {
let username: String
let name: String

required init?(response: NSHTTPURLResponse, representation: AnyObject) {
@objc required init?(response: NSHTTPURLResponse, representation: AnyObject) {
self.username = response.URL!.lastPathComponent!
self.name = representation.valueForKeyPath("name") as! String
}
Expand All @@ -665,7 +661,7 @@ final class User: ResponseObjectSerializable {

```swift
Alamofire.request(.GET, "http://example.com/users/mattt")
.responseObject { _, _, user: User?, _ in
.responseObject { (_, _, user: User?, _) in
println(user)
}
```
Expand All @@ -679,9 +675,9 @@ The same approach can also be used to handle endpoints that return a representat

extension Alamofire.Request {
public func responseCollection<T: ResponseCollectionSerializable>(completionHandler: (NSURLRequest, NSHTTPURLResponse?, [T]?, NSError?) -> Void) -> Self {
let serializer: Serializer = { request, response, data in
let responseSerializer = GenericResponseSerializer<[T]> { request, response, data in
let JSONSerializer = Request.JSONResponseSerializer(options: .AllowFragments)
let (JSON: AnyObject?, serializationError) = JSONSerializer(request, response, data)
let (JSON: AnyObject?, serializationError) = JSONSerializer.serializeResponse(request, response, data)

if let response = response, JSON: AnyObject = JSON {
return (T.collection(response: response, representation: JSON), nil)
Expand All @@ -690,9 +686,7 @@ extension Alamofire.Request {
}
}

return response(serializer: serializer) { request, response, object, error in
completionHandler(request, response, object as? [T], error)
}
return response(responseSerializer: responseSerializer, completionHandler: completionHandler)
}
}
```
Expand Down Expand Up @@ -725,7 +719,7 @@ extension Alamofire.Request {

```swift
Alamofire.request(.GET, "http://example.com/users")
.responseCollection { _, _, users: [User]?, _ in
.responseCollection { (_, _, users: [User]?, _) in
println(users)
}
```
Expand Down

0 comments on commit 86b95d0

Please sign in to comment.