将数组保存到文件目标C. [英] Saving Array To File Objective C

查看:184
本文介绍了将数组保存到文件目标C.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直坚持这一点,我终于想出来了,现在只是突然间它又停止了工作......

I've been stuck on this for ever and I finally figured it out and now just out of the blue it stopped working again...

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/scoreCards.dgs",documentsDirectory];

NSMutableArray *savedArrayOfScorecards = [[NSMutableArray alloc]init];
savedArrayOfScorecards = [NSMutableArray arrayWithContentsOfFile:filePath];
[savedArrayOfScorecards addObject:currentScoreCard];

[savedArrayOfScorecards writeToFile:filePath atomically:YES];

文件scoreCards.dgs甚至没有被创建......

我做错了什么?

推荐答案

可能有一对夫妇这里出了问题。

There could be a couple things going wrong here.

1)你在数组中存储的数据类型可能无法编码或存档到文件中。您包含的代码段并未提供有关您尝试保存的数据类型的良好提示。如果您的数组中有自定义对象(即不是NSString,NSNumber,NSDate等的东西),那么肯定是问题。 有很多问题 这里有关于StackOverflow 的信息,可以帮助您解决这个问题。

1) The kind of data you're storing in the array might not be encodable or archive-able to a file. And the code snippet you included doesn't give a good hint as to what kind of data you're trying to save. If you have custom objects in your array (i.e. things that are not NSString, NSNumber, NSDate, etc.), then that's definitely the problem. There are plenty of questions here on StackOverflow that might help you solve this issue.

2)你的数组的文件路径可能是假的。例如,您没有检查documentsDirectory是否为零或有效或可写。

2) Your array's filepath could be bogus. For example, you're not checking to see if "documentsDirectory" is nil or valid or writeable.

3)也可能,但不太可能, savedArrayOfScorecards 可能是一个零数组。您应该进行错误检查以确保实例化 savedArrayOfScorecards 并且数组中有多个对象。

3) Also possible, but not likely, "savedArrayOfScorecards" might be a nil array. You should do error checking to make sure "savedArrayOfScorecards" was instantiated and that there is more than one object in the array.

这篇关于将数组保存到文件目标C.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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