NSMutableArray问题 - iPhone

时间:2010-05-24 16:47:03

标签: iphone cocoa sdk nsmutablearray

我正在尝试让UITableView从文件中读取数据。我试过这样的事情:

NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *fullFileName = [NSString stringWithFormat:@"%@/entries.plist", documentsDirectory];

self.dataForTable = [[NSMutableArray alloc] initWithContentsOfFile:fullFileName];

这个编译很好,但是当在下面的代码片段中保存文件时,文件没有保存,也没有任何东西写入数组:

NSMutableDictionary*userDictionary;
userDictionary = [[NSMutableDictionary alloc] init];
[userDictionary setObject:name.text forKey:@"name"];
[userDictionary setObject:email.text forKey:@"email"];
[userDictionary setObject:serial.text forKey:@"serial"];
[userDictionary setObject:notes.text forKey:@"notes"];
[userDictionary setObject:[NSNumber numberWithInt:[licenseType selectedRowInComponent:0]] forKey:@"license_type"];
[userDictionary setObject:[date date] forKey:@"date"];
[userDictionary setObject:[NSNumber numberWithBool:[paymentSwitch isOn]] forKey:@"payment"];

NSString*dirToSaveTo = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

NSString*fileName = [NSString stringWithFormat:@"%@.plist",name.text];

NSString*saveName = [dirToSaveTo stringByAppendingPathComponent:fileName];

[userDictionary writeToFile:saveName atomically:NO];

NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *fullFileName = [NSString stringWithFormat:@"%@/entries.plist", documentsDirectory];

[self.dataForTable addObject:name.text];

NSLog(@"%@",self.dataForTable);

[self.dataForTable writeToFile:fullFileName atomically:YES];

NSLog只返回(null)。永远不会写出* plist文件。我做错了什么?

1 个答案:

答案 0 :(得分:2)

几乎可以肯定,这一行失败了,你不会检查它是否返回nil:

self.dataForTable = [[NSMutableArray alloc] initWithContentsOfFile:fullFileName];

一旦确实返回nil,其他每次调用都不会执行任何操作。最可能的问题是您构造的文件路径不正确,并且未指向该文件,或者该文件不是正确的plist。