0

所以我试图在我正在制作的应用程序的打开之间存储一些数据,但由于某种原因,我正在制作的目录和我正在存储的数据在应用程序打开之间没有持续存在,我无法计算找出原因……</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。任何人都可以帮助我了解为什么会这样吗?

4

1 回答 1

0

您需要在代码中进行一些额外的错误检查,以尝试缩小问题实际发生的位置。

像原始示例代码中的新行一样进行错误检查:

- (NSString *)pathForDataFile 
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"MyApp"];
    BOOL success = [[NSFileManager defaultManager] createDirectoryAtPath:documentsDirectory withIntermediateDirectories:YES attributes:nil error:&error];
    // success is NO only if there is an error.  success is YES if directory was created or if the directory already exists
    if(success == NO)
    {
        NSLog( @"error creating directory at path %@ is %@", documentsDirectory, [error localizedDescription] );
    }

    return documentsDirectory;
}

- (void)saveDataToDisk 
{
    NSString * path = [self pathForDataFile];
    if(([self usedPlaces] == NULL) || ([self usedPlaces count] == 0))
    {
        NSLog( @"usedPlaces is either null or has no entries");
    }
    NSDictionary * rootObject = [NSDictionary dictionaryWithObject: [self usedPlaces] forKey: @"key"];
    BOOL success = [NSKeyedArchiver archiveRootObject: rootObject toFile: path];
    if(success == NO)
    {
        NSLog( @"was not able to write data to path %@", path );
    }
}

- (void) loadDataFromDisk
{
    NSString     * path = [self pathForDataFile];
    NSDictionary * rootObject;

    rootObject = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    if(rootObject)
        [self setObject: [rootObject valueForKey:@"key"]];
    else
        NSLog( @"was not able to load data from file at %@", path );
}
于 2012-06-11T04:05:32.770 回答