如何将NSDictionary转换为NSData,反之亦然?


158

我正在发送NSStringUIImage使用蓝牙。我决定将两者都存储在中NSDictionary,然后将字典转换为NSData

我的问题是如何转换NSDictionaryNSData签证,反之亦然?

Answers:


361

NSDictionary-> NSData:

NSData *myData = [NSKeyedArchiver archivedDataWithRootObject:myDictionary];

NSData-> NSDictionary:

NSDictionary *myDictionary = (NSDictionary*) [NSKeyedUnarchiver unarchiveObjectWithData:myData];

对于NSData-> NSDictionary,强制转换为NSDictionary似乎是不必要的。

51

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 *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];

您可以使用符合NSCoding的任何类来执行此操作。

资源


1
不要从这个答案中
脱颖而出

10
是的,我的答案是从2011年起不存在的ARC
LeonS 2014年

41

使用NSJSONSerialization:

NSDictionary *dict;
NSData *dataFromDict = [NSJSONSerialization dataWithJSONObject:dict
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:&error];

NSDictionary *dictFromData = [NSJSONSerialization JSONObjectWithData:dataFromDict
                                                             options:NSJSONReadingAllowFragments
                                                               error:&error];

最新的return id,所以在转换后检查返回的对象类型是一个好主意(这里我转换为NSDictionary)。


23

Swift 2中,您可以通过以下方式进行操作:

var dictionary: NSDictionary = ...

/* NSDictionary to NSData */
let data = NSKeyedArchiver.archivedDataWithRootObject(dictionary)

/* NSData to NSDictionary */
let unarchivedDictionary = NSKeyedUnarchiver.unarchiveObjectWithData(data!) as! NSDictionary

Swift 3中

/* NSDictionary to NSData */
let data = NSKeyedArchiver.archivedData(withRootObject: dictionary)

/* NSData to NSDictionary */
let unarchivedDictionary = NSKeyedUnarchiver.unarchiveObject(with: data)

不工作 !Swift 3等效代码无法正常工作。让数据= NSKeyedArchiver.archivedData(withRootObject:dataFromNetwork)[_SwiftValue encodeWithCoder:]:无法识别的选择发送到实例...
mythicalcoder

感谢您的快速反应。我的意思是-Swift 3代码也不起作用。我的数据来自3lvis / networking HTTP库,它是Any?类型。但它显示的错误从转换NSDictionaryNSData。所以我尝试了您的代码。它不起作用。
mythicalcoder

17

来自NSData的NSDictionary

http://www.cocoanetics.com/2009/09/nsdictionary-from-nsdata/

NSDictionary转换为NSData

您可以为此使用NSPropertyListSerialization类。看一下它的方法:

+ (NSData *)dataFromPropertyList:(id)plist format:(NSPropertyListFormat)format
                              errorDescription:(NSString **)errorString

返回包含指定格式的给定属性列表的NSData对象。


对于大多数Mac OS开发人员来说,这是一个非常优雅的解决方案。
mjl007 '18

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.