简体   繁体   English

存储少量 NSMutableArrays(使用 plist)?

[英]Store Small Number of NSMutableArrays (with plist)?

I need store just 10 arrays in my app, which I can change from time to time.我只需要在我的应用程序中存储 10 个数组,我可以不时更改它们。 What would the best way to do this be?最好的方法是什么?

I have been looking into writing to a plist, is this a good idea?我一直在考虑写一个 plist,这是个好主意吗?

Each of the 10 arrays will have 10 objects (just strings). 10 个数组中的每一个都有 10 个对象(只是字符串)。

Using a plist file is a fine way to go as long as you don't need to interoperate with a non-Objective-C codebase (eg share with a Windows app, send the data to a server, etc.)只要您不需要与非 Objective-C 代码库进行互操作(例如,与 Windows 应用程序共享、将数据发送到服务器等),使用 plist 文件是一个很好的方法。

You could create an NSDictionary (or rather the mutable version in your case) and populate it with your arrays.您可以创建一个 NSDictionary (或者更确切地说是您的情况下的可变版本)并用您的数组填充它。 Then you could write them to disk with然后你可以将它们写入磁盘

writeToFile:atomically:

At start you could check whether your path is valid (meaning your dict exists) and assign it to your iVar in code for example with一开始你可以检查你的路径是否有效(意味着你的字典存在)并在代码中将它分配给你的 iVar 例如

initWithContentsOfFile:

The advantage is that the underlying representation is a plist for the NSDictionary (or NSMutableDictionary).优点是底层表示是 NSDictionary(或 NSMutableDictionary)的 plist。 So you are good to go here as well and as your objects within the arrays conform to NSCoding they all know how to encode them for writing to disk.所以你也可以去这里,因为数组中的对象符合NSCoding ,他们都知道如何对它们进行编码以写入磁盘。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM