Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.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/5/objective-c/26.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/1/visual-studio-2008/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
Ios 正确:是否返回零的有效数据?_Ios_Objective C - Fatal编程技术网

Ios 正确:是否返回零的有效数据?

Ios 正确:是否返回零的有效数据?,ios,objective-c,Ios,Objective C,需要澄清:我编写这个类方法是为了加载一个图像。如果图像不存在,返回nil是处理返回值的方法,还是返回未初始化的UIImage(仍然为nil但更清晰) 返回nil,这是指示操作无法完成的正确方式。。。 那么下面的代码就可以工作了: UIImage * someImage; if ((someImage = [YourClass loadImageByName:@"donkey"])) { //do something }else{ //failure } 如果你想,你也可以提供一些反馈 + (U

需要澄清:我编写这个类方法是为了加载一个图像。如果图像不存在,返回
nil
是处理返回值的方法,还是返回未初始化的
UIImage
(仍然为nil但更清晰)


返回nil,这是指示操作无法完成的正确方式。。。 那么下面的代码就可以工作了:

UIImage * someImage;
if ((someImage = [YourClass loadImageByName:@"donkey"]))
{
//do something
}else{
//failure
}
如果你想,你也可以提供一些反馈

+ (UIImage*)loadImageByName:(NSString*)name error:(NSError **)err
{
    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *imagePath = [documentsPath stringByAppendingPathComponent:name];
    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:imagePath];

    if (fileExists){
        UIImage* tmpImage = [UIImage imageWithContentsOfFile:imagePath];
        return tmpImage;
    }else{
        if(err)
         {
             *err = [NSError someErrorMethodHere...];
         }
    }
    return nil;
}

返回nil,这是指示操作无法完成的正确方式。。。 那么下面的代码就可以工作了:

UIImage * someImage;
if ((someImage = [YourClass loadImageByName:@"donkey"]))
{
//do something
}else{
//failure
}
如果你想,你也可以提供一些反馈

+ (UIImage*)loadImageByName:(NSString*)name error:(NSError **)err
{
    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *imagePath = [documentsPath stringByAppendingPathComponent:name];
    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:imagePath];

    if (fileExists){
        UIImage* tmpImage = [UIImage imageWithContentsOfFile:imagePath];
        return tmpImage;
    }else{
        if(err)
         {
             *err = [NSError someErrorMethodHere...];
         }
    }
    return nil;
}

这很常见。许多基础和UIKIT方法都是一样的。只要记录一下,一旦出错,该方法将返回
nil

作为建议,为了防止目录中可能出现的问题,您可以使用以下方法():

例:


这很常见。许多基础和UIKIT方法都是一样的。只要记录一下,一旦出错,该方法将返回
nil

作为建议,为了防止目录中可能出现的问题,您可以使用以下方法():

例:


返回
nil
可以很好地指示无法加载图像。nil是nil。如果将nil“赋值”给UIImage类型的变量,然后返回已为空的变量,则没有任何增益#返回
nil
可以很好地指示无法加载图像。nil是nil。如果将nil“赋值”给UIImage类型的变量,然后返回已为空的变量,则没有任何增益#
BOOL isdir;

if ( [[NSFileManager defaultManager] fileExistsAtPath:imagePath isDirectory:&isdir] && (! isdir) )
    // file exists and not a directory
else
    // handle error like mentioned in another answer