mardi 31 mars 2015

Swift : JSON into string into NSData Error unrecognized selector sent to instance

I'm a little stuck here. I'm creating an NSMutableDictionnary that I'm converting into JSON with SwiftyJSON, and then I want to collect that json into a string, transform into NSData in order to send it into a post request.


Here is my code



var dict:NSMutableDictionary = NSMutableDictionary()
dict.setValue(flight.id, forKey: "id")
dict.setValue(nil , forKey: "registration")
dict.setValue(60 , forKey: "duration")
dict.setValue(comment , forKey: "comment")

var json = JSON(dict).rawValue

let urlPath: String = "myawseomeurl"
var url: NSURL = NSURL(string: urlPath)!

var request: NSMutableURLRequest = NSMutableURLRequest(URL: url)

request.HTTPMethod = "POST"

let data = json.dataUsingEncoding(NSUTF8StringEncoding)

request.timeoutInterval = 60
request.HTTPBody=data
request.HTTPShouldHandleCookies=false

let queue:NSOperationQueue = NSOperationQueue()

NSURLConnection.sendAsynchronousRequest(request, queue: queue, completionHandler:{ (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in

var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary

})


And I get the following error:



[__NSDictionaryM dataUsingEncoding:]: unrecognized selector sent to instance 0x7fc9605e8ad0
2015-03-31 14:36:08.891 LearnToFly[18631:5221809] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryM dataUsingEncoding:]: unrecognized selector sent to instance 0x7fc9605e8ad0'

Aucun commentaire:

Enregistrer un commentaire