1

我一直被困在这个问题上,我终于想通了,现在它突然又停止工作了......

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 甚至没有被创建......

我究竟做错了什么?

4

5 回答 5

2

There could be a couple things going wrong here.

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) Your array's filepath could be bogus. For example, you're not checking to see if "documentsDirectory" is nil or valid or writeable.

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.

于 2012-06-22T15:25:13.023 回答
1

您的问题是,尽管您创建了一个数组,但在读取文件之前,它在您的调用中被 nil-ed:

savedArrayOfScorecards = [NSMutableArray arrayWithContentsOfFile:filePath];

所以,因为这个 savedArrayOfScorecards 现在是 nil,你把它写入文件的调用没有做任何事情。您应该将数组加载到另一个变量,并检查它是否为 nil,并且仅当从文件中读取的变量为 nil 时才创建新数组。像这样的东西:

NSMutableArray *savedArrayOfScorecards = [NSMutableArray arrayWithContentsOfFile:filePath];
if (!savedArrayOfScorecards) {
    savedArrayOfScorecards = [[NSMutableArray alloc]init];
}
于 2012-06-22T15:34:31.920 回答
0

您确定文件在加载时存在吗?

savedArrayOfScorecards = [NSMutableArray arrayWithContentsOfFile:filePath];

这一行从文件中创建了一个新的 NSMutableArray。如果文件不存在,则返回 nil。writeToFile然后被发送到 nil 并且什么都不会发生。

添加一个检查以查看它是否为 nil,如果是则创建一个新数组:

NSMutableArray *savedArrayOfScorecards = [NSMutableArray arrayWithContentsOfFile:filePath];
if(savedArrayOfScorecards == nil) savedArrayOfScorecards = [NSMutableArray array];
[savedArrayOfScorecards addObject:currentScoreCard];

[savedArrayOfScorecards writeToFile:filePath atomically:YES];
于 2012-06-22T15:28:41.230 回答
0

NSMutableArray 不是符合属性列表的格式。您必须使用NSArchiver使其符合 plist。

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:@"ALLLAALLAAALLA"];

NSMutableData *data = [NSMutableData data];
NSKeyedArchiver *archive = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
[archive encodeObject:savedArrayOfScorecards forKey:@"Scorecards"];
[archive finishEncoding];

BOOL result = [data writeToFile:filePath atomically:YES];

NSLog(result ? @"YES" : @"NO");

在此处输入图像描述

于 2012-06-22T15:29:33.250 回答
0

正确答案已经在这里,只是添加一个更好的解决方案:

NSFileManager* fileManager = [NSFileManager defaultManager];
NSMutableArray* array;

if ([fileManager fileExistsAtPath:filePath]) {
    array = [NSMutableArray arrayWithContentsOfFile:filePath];

    NSAssert(array != nil, @"Invalid data in file.");
}
else {
    array = [[NSMutableArray] alloc] init];
}

[array addObject:currentScoreCard];
[array writeToFile:filePath atomically:YES];

于 2012-06-22T15:47:00.057 回答