简体   繁体   English

该操作无法完成。 可可错误4

[英]The operation couldn’t be completed. Cocoa Error 4

I am having issues on a project i've taken over for a pervious developer that got halfway through and then left - the issues is that on a particular call to save an image from a url into core data it returns the error 我在一个项目上遇到了问题,我已经接手了一个上半身然后离开的前任开发人员 - 问题是在特定的调用中将图像从url保存到核心数据中它返回错误

The operation couldn't be completed. Cocoa Error 4.

From what I can see error 4 is NSFileNoSuchFileError - the code associated with it is 从我所看到的错误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]);
}

it is throwing the error in the log that conatains the world purple monkey! 它在日志中抛出了错误世界紫猴! - The odd thing is that it works in the original project - i have tried carious combinations of simulator, clearing simulator, running on devices etc, and still get the same result.. - 奇怪的是它在原始项目中有效 - 我尝试了模拟器,清除模拟器,在设备上运行等的龋齿组合,并且仍然得到相同的结果..

Thanks Guys. 多谢你们。

Here's the solution, due to popular request: 由于受欢迎的要求,这是解决方案:

This happens when the directory into which you are trying to save the file doesn't exist. 当您尝试保存文件的目录不存在时,会发生这种情况。 The way to get round it is to put in place checks as you save that create directories that don't already exist: 绕过它的方法是在保存时创建检查,创建尚不存在的目录:

ie

if (![[NSFileManager defaultManager] fileExistsAtPath:someDirPath]) {
    NSError *error = nil;
    if (![[NSFileManager defaultManager] createDirectoryAtPath:someDirPath withIntermediateDirecotries:YES attributes:nil error:&error]) {
        NSLog(@"Error: %@. %s, %i", error.localizedDescription, __PRETTY_FUNCTION__, __LINE__);
    }
}

EDIT: 编辑:

Here's the equivalent in Swift: 这是Swift中的等价物:

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

    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 该操作无法完成。 (可可错误:3840。) - The Operation couldn't be completed. (Cocoa error: 3840.) “这项行动无法完成。 (可可错误512.)“ - “The operation couldn’t be completed. (Cocoa error 512.)” 该操作无法完成。 (可可错误1560.) - The operation couldn’t be completed. (Cocoa error 1560.) 如何解决错误:操作无法完成。 (可可错误3840。)json iOS? - How to resolve error:the operation couldn’t be completed. (cocoa error 3840.) json iOS? 错误域= NSCocoaErrorDomain代码= 3840“操作无法完成。(可可错误3840.) - Error Domain = NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.) 无法保存到数据存储:无法完成操作。 (可可错误133020.) - Failed to save to data store: The operation couldn’t be completed. (Cocoa error 133020.) 该操作无法完成。 (可可错误3840。)”(在字符16周围的对象中没有键值。) - The operation couldn’t be completed. (Cocoa error 3840.)" (No value for key in object around character 16.) JSON NSJSONSerialization无法完成该操作。 (可可错误3840。) - JSON NSJSONSerialization The operation couldn’t be completed. (Cocoa error 3840.) iOS - Twitter连接问题:“操作无法完成。 (可可错误3840.)“ - iOS - Twitter Connectivity Issue: “The operation couldn’t be completed. (Cocoa error 3840.)” JSON解析错误:错误域= NSCocoaErrorDomain代码= 3840“该操作无法完成。(可可错误3840。) - JSON parse error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM