我试图写一个简单的数组到一个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;
}
我两次调用该方法,在第一它不应该找到文件,并写入它的第一次。 然后,第二个电话应该能够找到该文件,但事实并非如此。 任何人都可以看到我要去哪里错了吗?