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

Ali*_*Ali 156 iphone objective-c nsdictionary ios

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

我的问题是如何转换NSDictionary,NSData反之亦然?

小智 356

NSDictionary - > NSData:

NSData *myData = [NSKeyedArchiver archivedDataWithRootObject:myDictionary];
Run Code Online (Sandbox Code Playgroud)

NSData - > NSDictionary:

NSDictionary *myDictionary = (NSDictionary*) [NSKeyedUnarchiver unarchiveObjectWithData:myData];
Run Code Online (Sandbox Code Playgroud)


Leo*_*onS 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];
Run Code Online (Sandbox Code Playgroud)

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];
Run Code Online (Sandbox Code Playgroud)

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

资源

  • 是的,我的回答是从2011年ARC不存在的 (10认同)

use*_*109 40

使用NSJSONSerialization:

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

NSDictionary *dictFromData = [NSJSONSerialization JSONObjectWithData:dataFromDict
                                                             options:NSJSONReadingAllowFragments
                                                               error:&error];
Run Code Online (Sandbox Code Playgroud)

最新的返回id,所以在你演员之后检查返回的对象类型是个好主意(这里我已经转换为NSDictionary).


Luc*_*nzo 21

Swift 2中,你可以这样做:

var dictionary: NSDictionary = ...

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

/* NSData to NSDictionary */
let unarchivedDictionary = NSKeyedUnarchiver.unarchiveObjectWithData(data!) as! NSDictionary
Run Code Online (Sandbox Code Playgroud)

Swift 3中:

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

/* NSData to NSDictionary */
let unarchivedDictionary = NSKeyedUnarchiver.unarchiveObject(with: data)
Run Code Online (Sandbox Code Playgroud)


Jha*_*iya 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
Run Code Online (Sandbox Code Playgroud)

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