Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 操作无法’;不可能完成。可可错误4_Objective C_Cocoa - Fatal编程技术网

Objective c 操作无法’;不可能完成。可可错误4

Objective c 操作无法’;不可能完成。可可错误4,objective-c,cocoa,Objective C,Cocoa,我在一个项目上遇到了一些问题,我接手的前一个开发人员完成了一半,然后离开了。问题是,在一次将图像从url保存到核心数据的特定调用中,它返回了错误 The operation couldn't be completed. Cocoa Error 4. 从这里我可以看到错误4是NSFileNoSuchFileError-与它相关的代码是 if (iconURL) { NSURL *imageURL = [NSURL URLWithString:iconURL]; NSData *i

我在一个项目上遇到了一些问题,我接手的前一个开发人员完成了一半,然后离开了。问题是,在一次将图像从url保存到核心数据的特定调用中,它返回了错误

The operation couldn't be completed. Cocoa Error 4.
从这里我可以看到错误4是NSFileNoSuchFileError-与它相关的代码是

if (iconURL)
{
    NSURL *imageURL = [NSURL URLWithString:iconURL];
    NSData *img = [NSData dataWithContentsOfURL:imageURL];
    NSString *path = [self logoPath];
    NSLog(@" url path %@", imageURL);

NSError *error = nil;
BOOL success = [img writeToFile:path options:0 error:&error];
if (! success) {
    NSLog(@" purple monkey! %@", [error localizedDescription]);
}
}
else if ([self hasLogo])

{
    NSError *error = nil;
    [[NSFileManager defaultManager] removeItemAtPath:[self logoPath] error:&error];
    if (error)
        NSLog(@"Error removing old logo: %@", [error localizedDescription]);
}
这是在日志中抛出的错误,科纳塔因世界紫色猴子奇怪的是,它在最初的项目中起作用——我尝试了模拟机、清除模拟机、在设备上运行等多种组合,但仍然得到了相同的结果


谢谢大家。

鉴于普遍的要求,这里有一个解决方案:

当您试图保存文件的目录不存在时,就会发生这种情况。绕过它的方法是在保存时进行检查,以创建不存在的目录:

i、 e

编辑:

以下是Swift中的等效项:

if !NSFileManager.defaultManager().fileExistsAtPath("path") {
    do {
        try NSFileManager.defaultManager().createDirectoryAtPath("path", withIntermediateDirectories: true, attributes: nil)
    } catch _ {

    }
}

请添加日志的输出。没有日志-它只是说。无法完成该操作。Cocoa错误4。我知道已经有一段时间了,但我遇到了这个问题,对我来说,这是因为我试图保存文件的目录不存在。谢谢@RASS!你的建议帮我解决了这个问题。@RASS请把这个作为解决方案。对我来说,通过使用PRETTY u函数宏可以让你的生活更轻松。(前后两个下划线)谢谢你的提示。多年前,我从苹果公司实施的一个基于核心数据的应用程序的
AppDelegate.m
文件中得到了这个信息。你能提供swift中的示例吗?我的属性有问题,现在出现513错误(没有权限)。@mobibob注意,这个问题与错误4有关,即文件不存在错误。权限错误听起来像是您试图访问沙箱之外的内容。尽管如此,请参见Swift代码的编辑答案。
if !NSFileManager.defaultManager().fileExistsAtPath("path") {
    do {
        try NSFileManager.defaultManager().createDirectoryAtPath("path", withIntermediateDirectories: true, attributes: nil)
    } catch _ {

    }
}