I am sending NSString
and UIImage
using bluetooth. I decided to store both in a NSDictionary
and then convert the dictionary to NSData
.
My question is how to convert NSDictionary
to NSData
and visa versa?
I am sending NSString
and UIImage
using bluetooth. I decided to store both in a NSDictionary
and then convert the dictionary to NSData
.
My question is how to convert NSDictionary
to NSData
and visa versa?
NSDictionary -> NSData:
NSData -> NSDictionary
You can do that with any class that conforms to NSCoding.
source
In Swift 2 you can do it in this way:
In Swift 3:
Please try this one
Use NSJSONSerialization:
The latest returns
id
, so its a good idea to check the returned object type after you cast (here i casted to NSDictionary).NSDictionary -> NSData:
NSData -> NSDictionary:
NSDictionary from NSData
http://www.cocoanetics.com/2009/09/nsdictionary-from-nsdata/
NSDictionary to NSData
You can use NSPropertyListSerialization class for that. Have a look at its method:
Returns an NSData object containing a given property list in a specified format.