writeToFile给出NSUnderlyingError = 0x6a3d450“操作无法完成。是一个目录”

时间:2012-06-11 09:17:24

标签: objective-c ios cocoa grand-central-dispatch writetofile

我正在尝试将文件写入GCD中的路径,但它不起作用。继续收到错误“NSUnderlyingError = 0x6a3d450”操作无法完成。目录是“

dispatch_queue_t downloadQueue = dispatch_queue_create("flickr downloader", NO); 

dispatch_async(downloadQueue, ^{

    NSString* name = [photoDictionary valueForKey:@"title"];
    path = [path stringByAppendingPathComponent:id];

    if(![manager fileExistsAtPath:path]){
        if([manager createDirectoryAtPath: path withIntermediateDirectories:NO attributes:nil error:nil]){ //create intermediate directories
            data = [NSData dataWithContentsOfURL:[FlickrFetcher urlForPhoto:photoDictionary format:FlickrPhotoFormatOriginal]];  
            NSError *error = nil;
            BOOL success = [data writeToFile:path options:NSDataWritingAtomic error:&error];
            if (!success) {
                NSLog(@"Failed to write to file with error: %@", [error description]);
            }

            if([data writeToFile: path atomically:YES]){
                NSLog(@"data written to file");
            }   
        }
    }
    else{
        if([manager fileExistsAtPath:path]){
            data = [manager contentsAtPath:path];
        }
    }


    dispatch_async(dispatch_get_main_queue() , ^{
        sender.navigationItem.rightBarButtonItem = item;
        sender.photoData = data;
        sender.photoName = name; 
        //call the segue
        [sender performSegueWithIdentifier:segueTitle sender:sender];
    });       

});

dispatch_release(downloadQueue);

我得到的问题是永远不会执行[data writeToFile:path atomically:YES]。然而,确实创建了数据路径,当我“数据”时,程序有点挂起,所以我猜想在调用writeToFile时数据没有被完全取出。但是,我无法确切地指出问题,也不知道如何解决它。任何建议都可以非常感谢。谢谢!

1 个答案:

答案 0 :(得分:0)

您的代码正在您尝试下次创建文件的路径上创建目录。拿出来或改为创建父目录