2011-04-01 59 views

回答

334

的NSDictionary - > NSData的:

NSData *myData = [NSKeyedArchiver archivedDataWithRootObject:myDictionary]; 

的NSData - >的NSDictionary:

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

好辦法 - >的NSDictionary,投給NSDictionary中似乎沒有必要。 – user2387149 2015-07-20 01:59:33

+0

NSData的複雜字典 – Pang 2015-12-30 08:11:22

15

的NSDictionary從NSData的

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

的NSDictionary到NSData的

可以使用NSPropertyListSerialization類爲。看看它的方法:

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

返回一個包含指定格式的給定屬性列表的NSData對象。

49

的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任何類。

source

+1

不拿什麼都不是這個答案,但只是一個頭,它是不ARC兼容 – Madbreaks 2014-02-18 19:45:59

+7

是的,我的回答是從2011 ARC地方不存在存儲NSUserDefaults的上對於 – LeonS 2014-05-20 14:05:11

30

使用NSJSONSerialization:

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

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

最新收益id,所以它是一個好主意,檢查返回的對象類型你施放後(這裏我現澆到的NSDictionary)。

18

斯威夫特2,你可以這樣做:

var dictionary: NSDictionary = ... 

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

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

斯威夫特3

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

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

沒有工作! Swift 3等效代碼無效。 let data = NSKeyedArchiver。archivedData(withRootObject:dataFromNetwork) [_SwiftValue encodeWithCoder:]:無法識別的選擇器發送到實例... – mythicalcoder 2016-11-03 14:46:44

+0

@Maven我更新版本與Swift 3兼容 – 2016-11-03 14:52:21

+0

欣賞您的快速響應。我的意思是這個 - Swift 3代碼也沒有工作。我的數據來自3lvis/networking HTTP庫,它是'Any?'類型。但它顯示從'NSDictionary'到'NSData'的轉換錯誤。所以我嘗試了你的代碼。它不起作用。 – mythicalcoder 2016-11-03 15:50:36

0

請試試這個

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

它是'response',而不是'responce'。我爲你糾正了這個問題。 – 2017-03-22 21:48:20

相關問題