是否可以将NSMutableArray及其所有内容存储到文件中,然后再从那里还原? [英] Is it possible to store an NSMutableArray together with all its contents into a file and restore it later from there?

查看:67
本文介绍了是否可以将NSMutableArray及其所有内容存储到文件中,然后再从那里还原?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iPhone OS中可以使用某些序列化吗?这实际上可行吗,还是我应该很快忘记呢?

Some kind of serialization available in iPhone OS? Is that practically possible or should I quickly forget about that?

我正在制作一个微型应用程序,它将一些输入存储在NSMutableArray中.当用户离开时,输入应该保持活动状态,直到他/她返回以继续向/从该阵列添加或删除内容为止.

I am making a tiny app that stores some inputs in an NSMutableArray. When the user leaves, the inputs should stay alive until he/she returns to continue adding or removing stuff to/from that array.

当应用程序退出时,必须有某种方法可以将数组中的所有内容存储在一个文件中.还是我必须对其进行迭代,将所有内容都撕掉并写成它,即用逗号分隔,然后下次再次进入,读入内容,然后对文件中的行进行迭代以使用该数据构成一个数组?那将很难像一块砖头.怎么做?

When the app quits, there must be some way to store all the stuff in the array in a file. Or must I iterate over it, rip everything out and write it i.e. comma-separated somewhere, then the next time go in again, read the stuff in, and iterate over the lines in the file to make an array with that data? That would be hard like a brick. How to?

推荐答案

简单的方法,因为您已经有一个NSArray对象,所以可以使用

The easy way, since you already have an NSArray object is to write the array to disk using

- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag

并通过以下方式读回:

- (id)initWithContentsOfFile:(NSString *)aPath

+ (id)arrayWithContentsOfFile:(NSString *)aPath

您也可以使用NSCoder.

You can also use NSCoder.

您可能可以在sof中搜索正确的代码.

You can probably search sof for the right code.

这篇关于是否可以将NSMutableArray及其所有内容存储到文件中,然后再从那里还原?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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