我使用藍牙發送NSString
和UIImage
。我決定存儲在NSDictionary
,然後將字典轉換爲NSData
。如何將NSDictionary轉換爲NSData,反之亦然?
我的問題是如何將NSDictionary
轉換爲NSData
,反之亦然?
我使用藍牙發送NSString
和UIImage
。我決定存儲在NSDictionary
,然後將字典轉換爲NSData
。如何將NSDictionary轉換爲NSData,反之亦然?
我的問題是如何將NSDictionary
轉換爲NSData
,反之亦然?
的NSDictionary - > NSData的:
NSData *myData = [NSKeyedArchiver archivedDataWithRootObject:myDictionary];
的NSData - >的NSDictionary:
NSDictionary *myDictionary = (NSDictionary*) [NSKeyedUnarchiver unarchiveObjectWithData:myData];
的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對象。
的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任何類。
使用NSJSONSerialization:
NSDictionary *dict;
NSData *dataFromDict = [NSJSONSerialization dataWithJSONObject:dict
options:NSJSONWritingPrettyPrinted
error:&error];
NSDictionary *dictFromData = [NSJSONSerialization JSONObjectWithData:dataFromDict
options:NSJSONReadingAllowFragments
error:&error];
最新收益id
,所以它是一個好主意,檢查返回的對象類型你施放後(這裏我現澆到的NSDictionary)。
在斯威夫特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)
沒有工作! Swift 3等效代碼無效。 let data = NSKeyedArchiver。archivedData(withRootObject:dataFromNetwork) [_SwiftValue encodeWithCoder:]:無法識別的選擇器發送到實例... – mythicalcoder 2016-11-03 14:46:44
@Maven我更新版本與Swift 3兼容 – 2016-11-03 14:52:21
欣賞您的快速響應。我的意思是這個 - Swift 3代碼也沒有工作。我的數據來自3lvis/networking HTTP庫,它是'Any?'類型。但它顯示從'NSDictionary'到'NSData'的轉換錯誤。所以我嘗試了你的代碼。它不起作用。 – mythicalcoder 2016-11-03 15:50:36
請試試這個
NSError *error;
NSDictionary *responseJson = [NSJSONSerialization JSONObjectWithData:webData
options:NSJSONReadingMutableContainers
error:&error];
它是'response',而不是'responce'。我爲你糾正了這個問題。 – 2017-03-22 21:48:20
好辦法 - >的NSDictionary,投給NSDictionary中似乎沒有必要。 – user2387149 2015-07-20 01:59:33
NSData的複雜字典 – Pang 2015-12-30 08:11:22