本文介绍了将NSDictionary对象转换为NSData对象,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须将 NSDictionary
对象转换为 NSData
,而且我必须得到相同的 NSDictionary
超出 NSData
对象。我该怎么办呢?
I have to convert an NSDictionary
object into NSData
and further I have to get the same NSDictionary
out of the NSData
object. How should I go about it?
推荐答案
使用NSKeyedArchiver
use NSKeyedArchiver
到将NSDictionary转换为NSData
To convert NSDictionary To NSData
NSMutableData *data = [[NSMutableData alloc]init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
[archiver encodeObject:YOURDICTIONARY forKey: YOURDATAKEY];
archiver finishEncoding];
[data writeToFile:YOURFILEPATH atomically:YES];
[data release];
[archiver release];
从存储的NSData中获取NSDictionary
To get the NSDictionary back from the stored NSData
NSData *data = [[NSMutableData alloc]initWithContentsOfFile:YOURFILEPATH];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
YOURDICTIONARY = [unarchiver decodeObjectForKey: YOURDATAKEY];
[unarchiver finishDecoding];
[unarchiver release];
[data release];
这篇关于将NSDictionary对象转换为NSData对象,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!