简体   繁体   English

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

[英]How can I convert NSDictionary to NSData and vice versa?

I am sending NSString and UIImage using bluetooth. 我正在使用蓝牙发送NSStringUIImage I decided to store both in a NSDictionary and then convert the dictionary to NSData . 我决定将它们存储在NSDictionary ,然后将字典转换为NSData

My question is how to convert NSDictionary to NSData and visa versa? 我的问题是如何将NSDictionary转换为NSData ,反之亦然?

NSDictionary -> NSData: NSDictionary - > NSData:

NSData *myData = [NSKeyedArchiver archivedDataWithRootObject:myDictionary];

NSData -> NSDictionary: NSData - > NSDictionary:

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

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的任何类来执行此操作。

source 资源

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)。

In Swift 2 you 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)

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. 您可以使用NSPropertyListSerialization类。 Have a look at its method: 看看它的方法:

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

Returns an NSData object containing a given property list in a specified format. 返回包含指定格式的给定属性列表的NSData对象。

Please try this one 请试试这个

NSError *error;
NSDictionary *responseJson = [NSJSONSerialization JSONObjectWithData:webData
                                options:NSJSONReadingMutableContainers
                                error:&error];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM