iOS-NSData与NSDictionary的互相转换
這里,將字典轉換成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;
}
轉載于:https://www.cnblogs.com/XHShare/p/4973193.html
總結
以上是生活随笔為你收集整理的iOS-NSData与NSDictionary的互相转换的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 失眠害死人-jQueryAJAX
- 下一篇: 配置nginx,支持php的pathin