将 NSDictionary 对象转换为 NSData 对象,反之亦然

发布于 2024-09-08 04:19:55 字数 135 浏览 9 评论 0原文

我必须将 NSDictionary 对象转换为 NSData 并且进一步我必须从 NSData 中获取相同的 NSDictionary目的。我该怎么办?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

溺ぐ爱和你が 2024-09-15 04:19:55

使用 NSKeyedArchiver

将 NSDictionary 转换为 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

NSData *data = [[NSMutableData alloc]initWithContentsOfFile:YOURFILEPATH];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
YOURDICTIONARY = [unarchiver decodeObjectForKey: YOURDATAKEY];
[unarchiver finishDecoding];
[unarchiver release];
[data release];

use NSKeyedArchiver

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];

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];
梦明 2024-09-15 04:19:55

罗伯特的答案的一个更简单的版本:

[NSKeyedArchiver archiveRootObject:YOURDICTIONARY toFile:YOURFILEPATH];

并且,相应地:

YOURDICTIONARY = [NSKeyedUnarchiver unarchiveObjectWithFile:YOURFILEPATH];

或者回答最初设置的问题,而不将文件插入到事物中:

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:YOURDICTIONARY];
...
YOURDICTIONARY = [NSKeyedUnarchiver unarchiveObjectWithData:data];

都是工厂方法,所以无论有没有 ARC 都是相同的代码;所使用的方法自 OS X v10.2 起就可用,并且从第一天起就在 iOS 上可用。

A much simpler version of Robert's answer:

[NSKeyedArchiver archiveRootObject:YOURDICTIONARY toFile:YOURFILEPATH];

And, correspondingly:

YOURDICTIONARY = [NSKeyedUnarchiver unarchiveObjectWithFile:YOURFILEPATH];

Or to answer the question as originally set, without imputing a file into things:

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:YOURDICTIONARY];
...
YOURDICTIONARY = [NSKeyedUnarchiver unarchiveObjectWithData:data];

It's all factory methods so it's the same code with or without ARC; the methods used have been available since OS X v10.2 and on iOS since day one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文