【问题标题】:Exception if nil or <null>如果为 nil 或 <null> 则异常
【发布时间】:2012-01-09 07:28:12
【问题描述】:

我从 php 脚本中提取图片的位置,如果没有图片位置,它会在 JSON 输出中显示该位置的“null”。这在我的代码中引发了异常。这是不起作用的代码:

    NSString *piclocation = picloc;
if(piclocation == nil || @"null"){

}else{
NSString *mutableUrlString = @"https://www.mypage.com/uploads";
mutableUrlString = [mutableUrlString stringByAppendingString: piclocation];
NSLog(mutableUrlString);
NSURL *url = [NSURL URLWithString: mutableUrlString];
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]]; 
imageView.image = image;
}

错误是:由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“* -[NSNull length]: unrecognized selector sent to instance 0x98d5a0” 有什么建议吗?

【问题讨论】:

    标签: objective-c cocoa-touch iphone-sdk-3.0


    【解决方案1】:

    你需要稍微修正一下你的代码。

    if (piclocation == (id)[NSNull null] || piclocation.length == 0 || [piclocation isEqualToString:@"null"] )
    

    我想这会为你做的。

    【讨论】:

    • 谢谢先生。一个也是:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多