Using Swift and Alamofire, I'm trying to convert a single json response in an Object ((dictionary: [String: AnyObject])) with the append method,but when I'm trying it the compiler fails throwing this error :
cannot invoke initializer for type 'Object' with an argument list of type (dictionary:(String:AnyObject))
Any ideas to how can I solve it?
Important, the return of the getById function must be [Object]?
the json response
Optional(["name": object1, "_id": 5470def9e0c0be27780120d8,
"imageUrl": https://s3-eu-west-1.amazonaws.com/api-static/objects/5470def9e0c0b_180.png,
"location": {
city = Madrid;
country = Spain;
},
"desc": blablablabla.
])
class Object {
var id: String!
var name: String!
var imageUrl: String!
var location: [String: String]!
init(dictionary: [String: AnyObject]) {
id = dictionary["_id"] as? String
name = dictionary["name"] as? String
imageUrl = dictionary["imageUrl"] as? String
location = dictionary["location"] as? [String: String]
}
}
func getById(completionHandler: ([Object]?, NSError?) -> ()) {
Alamofire.request(Router.GetById("5470def9e0c0be27780121d7")).responseJSON { (request, response, json, error) in
var object = [Object]()
var jsonObj = json as? [String: AnyObject]
for dictionary in jsonObj!{
object.append(Object(dictionary:dictionary))
}
completionHandler(object, nil)
} else {
completionHandler(nil, error)
}
}
}