所以我试图在我正在制作的应用程序的打开之间存储一些数据,但由于某种原因,我正在制作的目录和我正在存储的数据在应用程序打开之间没有持续存在,我无法计算找出原因……</p>
我有三种方法:
- (NSString *)pathForDataFile
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"MyApp"];
NSLog(@"%@", documentsDirectory);
NSError *error;
NSLog(@"%i", (int)[[NSFileManager defaultManager] createDirectoryAtPath:documentsDirectory withIntermediateDirectories:YES attributes:nil error:&error]);
return documentsDirectory;
}
- (void)saveDataToDisk
{
NSString * path = [self pathForDataFile];
NSMutableDictionary * rootObject;
rootObject = [NSMutableDictionary dictionary];
[rootObject setValue: [self usedPlaces] forKey:@"key"];
[NSKeyedArchiver archiveRootObject: rootObject toFile: path];
}
- (void) loadDataFromDisk
{
NSString * path = [self pathForDataFile];
NSDictionary * rootObject;
rootObject = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
[self setObject: [rootObject valueForKey:@"key"]];
}
当我打印出在 中创建目录的结果时pathForDataFile
,每次打印 1,这意味着每次创建该目录时,即使它不应该能够,因为在一次调用后该目录应该已经存在。saveDataToDisk
被调用applicationWillTerminate
,并且loadDataFromDisk
被调用applicationDidBecomeActive
。任何人都可以帮助我了解为什么会这样吗?