iOS应用程序启动之间的目录不存在

时间:2012-06-11 03:53:52

标签: objective-c ios nsfilemanager

所以我试图在我正在制作的应用程序的开口之间存储一些数据,但由于某种原因,我正在创建的目录和我正在存储的数据在应用程序开放之间不存在,我可以弄清楚为什么......

我有三种方法:

- (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。有人可以帮我解释为什么会这样吗?

1 个答案:

答案 0 :(得分: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 );
}