首页 > 代码库 > iOS 字典和NSData之间转换
iOS 字典和NSData之间转换
1 //字典转NSData: 2 +(NSData *)returnDataWithDictionary:(NSDictionary*)dict 3 { 4 NSMutableData *data =http://www.mamicode.com/ [[NSMutableData alloc]init]; 5 NSKeyedArchiver* archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data]; 6 [archiver encodeObject:dict forKey:@"talkData"]; 7 [archiver finishEncoding]; 8 return data; 9 } 10 11 //NSData 转字典: 12 // NSData转dictonary 13 +(NSDictionary*)returnDictionaryWithDataPath:(NSData*)data 14 { 15 // NSData* data = http://www.mamicode.com/[[NSMutableData alloc]initWithContentsOfFile:path]; 拿路径文件 16 NSKeyedUnarchiver* unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data]; 17 NSDictionary* myDictionary = [unarchiver decodeObjectForKey:@"talkData"]; 18 [unarchiver finishDecoding]; 19 return myDictionary; 20 }
iOS 字典和NSData之间转换
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。