【问题标题】:writeToFile gives NSUnderlyingError=0x6a3d450 "The operation couldn’t be completed. Is a directory"writeToFile 给出 NSUnderlyingError=0x6a3d450 “操作无法完成。是一个目录”
【发布时间】:2012-06-11 09:17:23
【问题描述】:

我正在尝试将文件写入 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] 永远不会执行。但是,确实创建了数据路径,当我“po data”时,程序有点挂起,所以我猜想在调用 writeToFile 时数据没有完全获取。但是我无法准确定位问题,也不知道如何解决。任何建议都将不胜感激。谢谢!

【问题讨论】:

  • “po 路径”怎么样?上面的错误消息清楚地表明它正在尝试写入目录...您需要附加文件名。 “路径”是什么样的?
  • 当我的 po 路径我得到类似:/Users/tanchen/Library/Application Support/iPhone Simulator/5.0/Applications/7E26ED55-04F6-4B79-9C65-6A88FACF93C9/Library/Caches/71231321。 .. 71231321 是照片的唯一 ID。问题是错误消息实际上表明该目录已创建。我还将 71231321 更改为其他自定义字符串,例如 mytext.txt 但仍然没有任何运气)=
  • 我还将代码块移到 GCD 之外,以消除由它引起的任何并发症,但仍然没有工作/
  • @borrrden 抱歉在之前的 cmets 中忘记@你了

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


【解决方案1】:

您的代码正在您尝试下一步创建文件的路径中创建一个目录。将其取出或创建父目录

【讨论】:

  • 嘿,迈克,我相应地更改了我的代码,所以我在路径 (library/cache/myPhotos) 下创建目录并创建另一个 NSString 路径名称 storePhotoPath (library/cache/myPhoto/photoID) 但是我没有 createDirectoryAtPath在 storePhotoPath。但问题是::: if([manager isWritableFileAtPath:storedPhotoPath]) 返回 NO,因此我假设您不能写入路径,除非您为路径创建目录?期待您的回复谢谢!
  • 您几乎不应该尝试检查操作是否可行。只需尝试操作,看看会发生什么
猜你喜欢
  • 2014-10-22
  • 2012-12-24
  • 2021-12-22
  • 2019-05-27
  • 1970-01-01
  • 2013-10-31
  • 2013-07-25
  • 2013-03-23
  • 1970-01-01
相关资源
最近更新 更多