dimanche 10 juillet 2016

How do I store serialize an NSDictionary with NSJSONSerialization when one of the values is NSData?


Take this simplified example:

let dict: [String: AnyObject] = ["foo": ("bar" as NSString).dataUsingEncoding(NSUTF8StringEncoding)!9]

let json = try! NSJSONSerialization.dataWithJSONObject(dict, options: [])

I cannot get that to run, it crashes with:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (NSConcreteMutableData)'

I need to send a block of JSON to my server, where some values are strings and some are NSData. What am I doing wrong?


Aucun commentaire:

Enregistrer un commentaire