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

iOS-NSData与NSDictionary的互相转换

时间:2015-11-18 00:43:33      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:

这里,将字典转换成NSData类型。有一点必须注意的,必须实现

- (id)initWithCoder:(NSCoder *)aDecoder方法和

- (void)encodeWithCoder:(NSCoder *)aCoder方法。

 

 

+ (NSData *)returnDataWithDictionary:(NSDictionary*)dict

{

    NSMutableData* data = [[NSMutableData alloc]init];

    NSKeyedArchiver* archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];

    [archiver encodeObject:dict forKey:@"talkData"];

    [archiver finishEncoding];

    return data;

}

将字典路径的字符串转换为字典 

+ (NSDictionary *)returnDictionaryWithDataPath:(NSString*)path

{

    NSData* data = [[NSMutableData alloc]initWithContentsOfFile:path];

    NSKeyedUnarchiver* unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];

    NSDictionary* myDictionary = [unarchiver decodeObjectForKey:@"talkData"];

    [unarchiver finishDecoding];

   

    return myDictionary;

}

iOS-NSData与NSDictionary的互相转换

标签:

原文地址:http://www.cnblogs.com/XHShare/p/4973193.html

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