我正在尝试将一个简单的数组写入 plist 文件,然后再检索它。我有以下代码:
+ (NSString*) dataFilePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *dataFilePath = [documentDirectory stringByAppendingPathComponent:@"TipAlertViewDefaults.plist"];
return dataFilePath;
}
+ (NSArray*) tipAlertViewDefaults
{
NSString *dataFilePath = [self dataFilePath];
NSLog(@"DataFilePath: %@", dataFilePath);
NSMutableArray *tipAlertViewDefaults;
if ([[NSFileManager defaultManager] fileExistsAtPath:dataFilePath])
{
NSLog(@"File Exists");
tipAlertViewDefaults = [[NSMutableArray alloc] initWithContentsOfFile:dataFilePath];
}
else
{
NSLog(@"File Doesn't Exist");
tipAlertViewDefaults = [[NSMutableArray alloc] initWithObjects:[NSNumber numberWithBool:NO], nil];
[tipAlertViewDefaults writeToFile:dataFilePath atomically:YES];
}
return tipAlertViewDefaults;
}
我调用了这个方法两次,第一次它不应该找到文件并第一次写入。然后第二次调用应该能够找到该文件,但它不是。谁能看到我要去哪里错了?