码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 字典和NSData之间转换

时间:2017-02-13 18:26:46      阅读:519      评论:0      收藏:0      [点我收藏+]

标签:hive   文件   cti   decode   contents   encode   之间   with   writing   

 1 //字典转NSData:
 2 +(NSData *)returnDataWithDictionary:(NSDictionary*)dict
 3 {
 4     NSMutableData *data = [[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 = [[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之间转换

标签:hive   文件   cti   decode   contents   encode   之间   with   writing   

原文地址:http://www.cnblogs.com/kfgcs/p/6387436.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!