什么是存储可可的对象数组的最好的方法? [英] What is the best way to store an array of objects with cocoa?

查看:119
本文介绍了什么是存储可可的对象数组的最好的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NSMutableArray与许多对象。一些是NSStrings,其他是NSMutableArrays,其他是NSNumbers。什么是存储此数据的最佳方式,以便应用程序可以再次使用它?

I have an NSMutableArray with many objects in it. Some are NSStrings, others are NSMutableArrays, others are NSNumbers. What is the best way to store this data so that the app could use it again?

数组也需要保持顺序。

我在想一个plist或NSUserDefaults?

I'm thinking a plist or NSUserDefaults?

非常感谢

推荐答案

请考虑使用 NSKeyedArchiver

// Archive
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:theArray];
NSString *path = @"/Users/Anne/Desktop/archive.dat";
[data writeToFile:path options:NSDataWritingAtomic error:nil];

// Unarchive
NSString *path = @"/Users/Anne/Desktop/archive.dat";
NSMutableArray *theArray = [NSKeyedUnarchiver unarchiveObjectWithFile:path];

它非常有用,特别是当数组包含更多字符串和数字时。

这种方式可以确保未归档的数组与原始数组相同。

It works great, especially in case the array contains more then strings and numbers.
This way you can be sure the unarchived array is identical to the original.

这篇关于什么是存储可可的对象数组的最好的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆