I am using Alamofire to make requests to a JSON API. I am trying to serialize a collection of Post objects that have an author object and a comments array inside of it.
I have done the following:
Step 1: Follow steps
Extended the Alamofire.Request
object and added the ResponseObjectSerializer
and ResponseCollectionSerializer
as explained in the documentation under Generic Response Object Serialization
Step 2: Add the following models
Post.swift
final class Post : ResponseObjectSerializable, ResponseCollectionSerializable {
let id: Int
let title: String
let body: String
let author: Author
let comments: [Comment]
required init?(response: NSHTTPURLResponse, representation: AnyObject) {
self.id = representation.valueForKeyPath("id") as Int
self.body = representation.valueForKeyPath("body") as String
self.title = representation.valueForKeyPath("title") as String
// What do I do with the author object
var authorObj: AnyObject? = representation.valueForKeyPath("author")
if (authorObj != nil) {
self.author = Author(response: response, representation: authorObj!)!
}
// What do I do with the comments Array?
self.comments = Comment.collection(response: response, representation: representation.valueForKeyPath("comments")!)
}
class func collection(#response: NSHTTPURLResponse, representation: AnyObject) -> [Post] {
var postList:[Post] = []
for p in representation as [AnyObject] {
postList.append(Post(response: response, representation: p)!)
}
return postList
}
}
Comment.swift
final class Comment : ResponseObjectSerializable, ResponseCollectionSerializable {
let id: Int
let body: String
required init?(response: NSHTTPURLResponse, representation: AnyObject) {
self.id = representation.valueForKeyPath("id") as Int
self.body = representation.valueForKeyPath("body") as String
}
class func collection(#response: NSHTTPURLResponse, representation: AnyObject) -> [Comment] {
var commentList:[Comment] = []
var commentArray = representation as [AnyObject]
for c in commentArray {
commentList.append(Comment(response: response, representation: c)!)
}
return commentList
}
}
Author.swift
final class Author : ResponseObjectSerializable {
let id: Int
let name: String
required init?(response: NSHTTPURLResponse, representation: AnyObject) {
self.id = representation.valueForKeyPath("id") as Int
self.name = representation.valueForKeyPath("name") as String
}
}
Step 3: The representation is a Builtin.RawPointer
(lldb) po representation
(instance_type = Builtin.RawPointer = 0x00007f8b9ae1d290 -> 0x000000010c7f4c88 (void *)0x000000010c7f4dc8: __NSArrayI)
Any suggestions?
Step 4: Here is how I am calling the code
class NetworkPostProvider {
typealias RequestsCollectionResponse = (NSError?, [Post]?) -> Void
class func all(onCompletion: RequestsCollectionResponse) {
var manager = Alamofire.Manager.sharedInstance
manager.session.configuration.HTTPAdditionalHeaders = [
"Authorization": NSUserDefaults.standardUserDefaults().valueForKey(DEFAULTS_TOKEN) as String
]
manager.request(.GET, BASE_URL + "/alamofire/nested")
.validate()
.responseCollection({ (req, res, requests:[Post]?, error) -> Void in
println("Request:")
println(req)
println("Response:")
println(res)
println(requests)
})
}
}