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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:25:11  来源:igfitidea点击:

How can I convert NSDictionary to NSData and vice versa?

iphoneobjective-ciosnsdictionary

提问by Ali

I am sending NSStringand UIImageusing bluetooth. I decided to store both in a NSDictionaryand then convert the dictionary to NSData.

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

My question is how to convert NSDictionaryto NSDataand visa versa?

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

回答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 的类来做到这一点。

source

来源

回答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];