Iphone 找不到plist文件
我试图将一个简单数组写入plist文件,然后稍后检索它。我有以下代码:Iphone 找不到plist文件,iphone,ios,cocoa-touch,Iphone,Ios,Cocoa Touch,我试图将一个简单数组写入plist文件,然后稍后检索它。我有以下代码: + (NSString*) dataFilePath { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES); NSString *documentDirectory = [paths objectAtIndex:0]; NSString *dataF
+ (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;
}
我调用这个方法两次,第一次它不应该找到文件并第一次写入它。然后,第二个调用应该能够找到该文件,但它没有找到。有人知道我哪里出错了吗?再说一次,Xcode愚蠢的自动完成让你迷路了:
NSDocumentationDirectory
应该是NSDocumentDirectory
iOS上不存在文档目录,因此您无法在其中写入。同样,Xcode愚蠢的自动完成功能使您丢失:
NSDocumentationDirectory
应该是NSDocumentDirectory
iOS上不存在文档目录,因此无法在其中写入。是否确实要在
NSDocumentationDirectory
目录中写入?我认为,这个目录不是默认创建的(所以您必须在创建之前创建它)
也许您想要
NSDocumentDirectory
。我试过了,你的代码运行正常。你真的想在NSDocumentationDirectory
目录中编写吗?我认为,这个目录不是默认创建的(所以您必须在创建之前创建它)
也许您想要
NSDocumentDirectory
。我试过了,你的代码运行正常。你说的“不是”是什么意思?应该输入您的if
/else
的哪个分支,以及实际输入的是哪个分支?您是否收到任何错误消息?(另外,这是一个语法错误:return[dataFilePath];
)return[dataFilePath]代码>-这不可编译。你说“它不是”是什么意思?应该输入您的if
/else
的哪个分支,以及实际输入的是哪个分支?您是否收到任何错误消息?(另外,这是一个语法错误:return[dataFilePath];
)return[dataFilePath]代码>-这不可编译。(使用vi
和make
?)开始构建iOS应用程序的另一个原因是检查应用程序文档目录中的文件会告诉你它没有被写入其中:`/users//library/application support/iphone simulator//Applications/。。。你必须通过ID搜索你的应用程序,但你会在那里找到它。是的,这看起来是个问题。使用NSDocumentationDirectory
返回一个路径,但该目录不存在,因此除非首先手动创建目录,否则任何文件访问都将失败。(使用vi
和make
开始构建iOS应用程序的另一个原因是什么?)检查应用程序文档目录中的文件会告诉您该文件没有被写入其中:`/users//library/application support/iphone simulator//Applications/。。。你必须通过ID搜索你的应用程序,但你会在那里找到它。是的,这看起来是个问题。使用NSDocumentationDirectory
返回一个路径,但该目录不存在,因此任何文件访问都将失败,除非首先手动创建该目录。