且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将NSMutableArray写入可可文件的问题

更新时间:2022-11-15 15:17:01

NSValues不能保存在plist(这是什么writeToFile:atomically:)。查看此处您可以保存的值。 (NSNumber是一种NSValue,你可以保存,但其他NSValues将失败。)

NSValues can't be saved in a plist (which is what writeToFile:atomically: does). Take a look here for the values you can save. (NSNumber is a kind of NSValue you can save, but other NSValues will fail.)

如果你想用NSValues保存数组,你可以使用归档,而不是writeToFile:atomically :. NSArray和NSValue都支持归档,因此您只需将数组转换为归档并将该数据保存到文件即可。 (它将包括NSValues)。代码看起来像这样:

If you want to save your array with NSValues, you can use archiving instead of writeToFile:atomically:. NSArray and NSValue both support archiving, so you just convert the array to an archive and save that data to a file. (It will include the NSValues as well.) The code looks something like this:

[NSKeyedArchiver archiveRootObject:myArray toFile:@"myPath"];

要加载它,只需使用:

NSArray *myArray = [NSKeyedUnarchiver unarchiveObjectWithFile:@"myPath"];