ios 如何将 NSDictionary 转换为 NSData,反之亦然?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5513075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I convert NSDictionary to NSData and vice versa?
提问by Ali
I am sending NSString
and UIImage
using bluetooth. I decided to store both in a NSDictionary
and then convert the dictionary to NSData
.
我正在发送NSString
和UIImage
使用蓝牙。我决定将两者都存储在 a 中NSDictionary
,然后将字典转换为NSData
.
My question is how to convert NSDictionary
to NSData
and visa versa?
我的问题是如何转换NSDictionary
为NSData
,反之亦然?
回答by Anh Nguyen
NSDictionary -> NSData:
NSDictionary -> NSData:
NSData *myData = [NSKeyedArchiver archivedDataWithRootObject:myDictionary];
NSData -> NSDictionary:
NSData -> NSDictionary:
NSDictionary *myDictionary = (NSDictionary*) [NSKeyedUnarchiver unarchiveObjectWithData:myData];
回答by LeonS
NSDictionary -> NSData:
NSDictionary -> NSData:
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:yourDictionary forKey:@"Some Key Value"];
[archiver finishEncoding];
[archiver release];
// Here, data holds the serialized version of your dictionary
// do what you need to do with it before you:
[data release];
NSData -> NSDictionary
NSData -> NSDictionary
NSData *data = [[NSMutableData alloc] initWithContentsOfFile:[self dataFilePath]];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
NSDictionary *myDictionary = [[unarchiver decodeObjectForKey:@"Some Key Value"] retain];
[unarchiver finishDecoding];
[unarchiver release];
[data release];
You can do that with any class that conforms to NSCoding.
你可以用任何符合 NSCoding 的类来做到这一点。
回答by user1244109
Use NSJSONSerialization:
使用 NSJSONSerialization:
NSDictionary *dict;
NSData *dataFromDict = [NSJSONSerialization dataWithJSONObject:dict
options:NSJSONWritingPrettyPrinted
error:&error];
NSDictionary *dictFromData = [NSJSONSerialization JSONObjectWithData:dataFromDict
options:NSJSONReadingAllowFragments
error:&error];
The latest returns id
, so its a good idea to check the returned object type after you cast (here i casted to NSDictionary).
最新返回id
,因此在转换后检查返回的对象类型是个好主意(这里我转换为 NSDictionary)。
回答by Luca Davanzo
In Swift 2you can do it in this way:
在Swift 2 中,你可以这样做:
var dictionary: NSDictionary = ...
/* NSDictionary to NSData */
let data = NSKeyedArchiver.archivedDataWithRootObject(dictionary)
/* NSData to NSDictionary */
let unarchivedDictionary = NSKeyedUnarchiver.unarchiveObjectWithData(data!) as! NSDictionary
In Swift 3:
在Swift 3 中:
/* NSDictionary to NSData */
let data = NSKeyedArchiver.archivedData(withRootObject: dictionary)
/* NSData to NSDictionary */
let unarchivedDictionary = NSKeyedUnarchiver.unarchiveObject(with: data)
回答by Jhaliya
NSDictionary from NSData
来自 NSData 的 NSDictionary
http://www.cocoanetics.com/2009/09/nsdictionary-from-nsdata/
http://www.cocoanetics.com/2009/09/nsdictionary-from-nsdata/
NSDictionary to NSData
NSDictionary 到 NSData
You can use NSPropertyListSerialization class for that. Have a look at its method:
您可以为此使用 NSPropertyListSerialization 类。看看它的方法:
+ (NSData *)dataFromPropertyList:(id)plist format:(NSPropertyListFormat)format
errorDescription:(NSString **)errorString
Returns an NSData object containing a given property list in a specified format.
返回包含指定格式的给定属性列表的 NSData 对象。
回答by Vijay Kachhadiya
Please try this one
请试试这个
NSError *error;
NSDictionary *responseJson = [NSJSONSerialization JSONObjectWithData:webData
options:NSJSONReadingMutableContainers
error:&error];