Bläddra i källkod

Improved generic response serializer example and updated to Swift 1.2.

Christian Noon 10 år sedan
förälder
incheckning
02625150e9
1 ändrade filer med 35 tillägg och 7 borttagningar
  1. 35 7
      README.md

+ 35 - 7
README.md

@@ -581,8 +581,9 @@ extension Alamofire.Request {
         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)
+
+            if let response = response, JSON: AnyObject = JSON {
+                return (T(response: response, representation: JSON), nil)
             } else {
                 return (nil, serializationError)
             }
@@ -601,8 +602,8 @@ final class User: ResponseObjectSerializable {
     let name: String
 
     required init?(response: NSHTTPURLResponse, representation: AnyObject) {
-        self.username = response.URL!.lastPathComponent
-        self.name = representation.valueForKeyPath("name") as String
+        self.username = response.URL!.lastPathComponent!
+        self.name = representation.valueForKeyPath("name") as! String
     }
 }
 ```
@@ -618,7 +619,7 @@ The same approach can also be used to handle endpoints that return a representat
 
 ```swift
 @objc public protocol ResponseCollectionSerializable {
-    class func collection(#response: NSHTTPURLResponse, representation: AnyObject) -> [Self]
+    static func collection(#response: NSHTTPURLResponse, representation: AnyObject) -> [Self]
 }
 
 extension Alamofire.Request {
@@ -626,8 +627,9 @@ extension Alamofire.Request {
         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.collection(response: response!, representation: JSON!), nil)
+
+            if let response = response, JSON: AnyObject = JSON {
+                return (T.collection(response: response, representation: JSON), nil)
             } else {
                 return (nil, serializationError)
             }
@@ -640,6 +642,32 @@ extension Alamofire.Request {
 }
 ```
 
+```swift
+@objc final class User: ResponseObjectSerializable, ResponseCollectionSerializable {
+    let username: String
+    let name: String
+
+    required init?(response: NSHTTPURLResponse, representation: AnyObject) {
+        self.username = response.URL!.lastPathComponent!
+        self.name = representation.valueForKeyPath("name") as! String
+    }
+
+    static func collection(#response: NSHTTPURLResponse, representation: AnyObject) -> [User] {
+        var users: [User] = []
+
+        if let representation = representation as? [[String: AnyObject]] {
+            for userRepresentation in representation {
+                if let user = User(response: response, representation: userRepresentation) {
+                    users.append(user)
+                }
+            }
+        }
+
+        return users
+    }
+}
+```
+
 ```swift
 Alamofire.request(.GET, "http://example.com/users")
          .responseCollection { (_, _, users: [User]?, _) in