0

尝试将文件保存到磁盘时出现以下错误:

ERROR IS Error Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t be completed. (Cocoa error 512.)" UserInfo=0x8d44150 {NSFilePath=/Users/test/Library/Application Support/iPhone Simulator/5.1/Applications/C283C4FF-645A-4C97-BB87-9591ECC57B0D/Library/Caches/newsfeedCache, NSUnderlyingError=0x8d25870 "The operation couldn’t be completed. Is a directory"}

NSMutableData *data = [[NSMutableData alloc] init];
        NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];  

        NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"test", @"hula", nil];

        [archiver encodeObject:dict forKey:kDataKey];
        [archiver finishEncoding];

        NSLog(@"CACHE PATH IS %@", self.newsfeedCachePath_);

        NSError *error = nil;
        BOOL success = [data writeToFile:self.newsfeedCachePath_ options:NSDataWritingAtomic error:&error];
        if (success && !error){

        } else {
            FNLog(@"ERROR IS %@", [error description]);
        }

        [archiver release];
        [data release];

这是我创建路径的方式:

- (NSString *)createDataPath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *dataPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:cacheName];

    /* check for existence of cache directory */
    if ([[NSFileManager defaultManager] fileExistsAtPath:dataPath]) {
        return dataPath;
    }

    NSError *error = nil;
    /* create a new cache directory */
    if (![[NSFileManager defaultManager] createDirectoryAtPath:dataPath
                                   withIntermediateDirectories:NO
                                                    attributes:nil
                                                         error:&error]) {
        DLog(@"Unresolved error %@, %@", error, [error userInfo]);
        return nil;
    }    

    NSString *path = [dataPath stringByAppendingPathComponent:cachePath];

    return path;
}

我究竟做错了什么?

4

1 回答 1

0

使用 Finder,导航到Home/Library/Application Support/iPhone Simulator/5.1/Applications/C283C4FF-645A-4C97-BB87-9591ECC57B0D/Library/Caches。真的newsfeedCache是目录吗?

编辑: [data writeToFile:] 需要一个文件名,而不是目录名。如果你给它提供一个目录名,那是一个错误。你必须想出某种数据存储方案。在不知道您的申请细节的情况下,我无法为您提供。您将在该缓存下拥有一个文件还是多个文件?以后你打算如何找回它们?等等。这些都是反问。自己回答它们,并根据这些答案,提出一个文件命名方案。

于 2012-06-15T17:32:13.573 回答