I am having issues sending my parameters over Alamofire. This is the JSON that the server expects:
{
“users”:[{“_id”:”1234567”}, {“_id”:”665543”}]
}
Now, in my Swift code, this is how I’m trying to build out the above object:
let parameters = [“users” : users]
return sendRequest(.PUT, url: “example.com", parameters: parameters)
where users is an array. Here is more info on what users is (I have taken out some of the other inherits to simplify):
class User : MyBase, CustomStringConvertible {
var name: String
var description : String {
get{
return ["_id" : self._id!].description
}
}
}
class MyBase {
var _id: String?
}
If I do a debugPrint(users)
, this is what I get:
[“users”: [["_id": "1234567"], ["_id": "665543"]]]
And here’s the sendRequest method that does the Alamofire stuff:
static func sendRequest(method: Alamofire.Method, url: String, parameters: [String: AnyObject]?) {
Alamofire.request(method, url, parameters: parameters as! [String: [User]], encoding: .JSON)
.validate()
.responseObject { (response: Response<User, NSError>) in
switch response.result {
case .Success(let value):
// do something
case .Failure(let error):
// do something else
}
}
}
And the error it throws on the Alamofire.request line is this:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (MyApp.User)’
Ultimately, what I have is an array of user objects, and I need to send that to my server in the JSON format mentioned above. Being new to Swift, I’ve been struggling trying to figure this out, so any help is appreciated.
Aucun commentaire:
Enregistrer un commentaire