FI可以在模拟器中保存和加载字典数据,但是当我在iPhone上运行时,它似乎不起作用。我不知道发生了什么。以下是我保存和加载数据的方法:
装载
NSString *homeDir = NSHomeDirectory();
NSDictionary *wholeDictionary = [NSDictionary dictionaryWithContentsOfFile:[NSString stringWithFormat:@"%@userdata.dat",homeDir]];
保存
NSString *homeDir = NSHomeDirectory();
[dict writeToFile:[NSString stringWithFormat:@"%@userdata.dat",homeDir] atomically:YES];
答案 0 :(得分:2)
NSHomeDirectory
返回不可写的应用程序目录。您需要将数据写入应用程序的文档目录:
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES) objectAtIndex:0];
[dict writeToFile:[documentsPath stringByAppendingPathComponent:@"userdata.dat"] atomically:YES];