objective-c 保存一个 NSArray

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1487606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:15:44  来源:igfitidea点击:

Saving a NSArray

objective-ccocoa

提问by Joshua

I would like to save an NSArray either as a file or possibly use user defaults. Here's what I am hoping to do.

我想将 NSArray 保存为文件或可能使用用户默认值。这就是我希望做的。

  1. Retrieve already saved NSArray (if any).
  2. Do something with it.
  3. Erase saved data (if any).
  4. Save the NSArray.
  1. 检索已保存的 NSArray(如果有)。
  2. 用它做点什么。
  3. 擦除保存的数据(如果有)。
  4. 保存 NSArray。

Is this possible, and if so how should I do this?

这可能吗,如果可以,我应该怎么做?

回答by rluba

NSArray provides you with two methods to do exactly what you want: initWithContentsOfFile:and writeToFile:atomically:

NSArray 为你提供了两种方法来做你想做的事情:initWithContentsOfFile:writeToFile:atomically:

A short example might look like this:

一个简短的示例可能如下所示:

//Creating a file path under iOS:
//1) Search for the app's documents directory (copy+paste from Documentation)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//2) Create the full file path by appending the desired file name
NSString *yourArrayFileName = [documentsDirectory stringByAppendingPathComponent:@"example.dat"];

//Load the array
NSMutableArray *yourArray = [[NSMutableArray alloc] initWithContentsOfFile: yourArrayFileName];
if(yourArray == nil)
{
    //Array file didn't exist... create a new one
    yourArray = [[NSMutableArray alloc] initWithCapacity:10];

    //Fill with default values
}
...
//Use the content
...
//Save the array
[yourArray writeToFile:yourArrayFileName atomically:YES];

回答by dstnbrkr

You could implement NSCodingon the objects the array contains and use NSKeyedArchiverto serialize/deserialize your array to disk.

您可以在数组包含的对象上实现NSCoding,并使用NSKeyedArchiver将数组序列化/反序列化到磁盘。

BOOL result = [NSKeyedArchiver archiveRootObject:myArray toFile:path];

The archiver will defer to your NSCoding implementation to get serializable values from each object and write a file that can be read with NSKeyedUnarchiver:

存档器将遵循您的 NSCoding 实现以从每个对象获取可序列化的值并写入一个可以使用NSKeyedUnarchiver读取的文件:

id myArray = [NSKeyedUnarchiver unarchiveObjectWithFile:path];

More info in the serialization guide.

序列化指南中的更多信息。

回答by Ian Turner

This would seem to be a problem most suited to Core Data as this will deal with all the persistent object data. When you retrieve you data it will return an NSSet, which is unsorted so you will have to have some way of sorting the data in the array such as a unique id number assocaited with each object you create.

这似乎是最适合 Core Data 的问题,因为这将处理所有持久对象数据。当您检索数据时,它将返回一个未排序的 NSSet,因此您必须有某种方式对数组中的数据进行排序,例如与您创建的每个对象相关联的唯一 ID 号。