将NSMutableArray写入可可文件的问题 [英] problem writing a NSMutableArray to file in cocoa

查看:175
本文介绍了将NSMutableArray写入可可文件的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个真正的初学者问题。
我有一个NSView子类,其中我创建一个NSMutableArray包含NSValues。当我想使用writetofile:将数组写入文件:atomatically:文件被创建,但它不包含可变数组包含的NSValues。
有人知道我如何成功地将这个可变数组写入文件?

A real beginners question. I have a NSView subclass in which I create a NSMutableArray containing NSValues. When I want to write the array to a file using writetofile:atomatically: the file is created but it contains none of the NSValues that the mutable array does contain. Does anyone know how I successfully can write this mutable array to a file?

感谢

推荐答案

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

这篇关于将NSMutableArray写入可可文件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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