Exception if nil or <null>

二次信任 提交于 2020-01-03 04:51:06

问题


Im pull the location of a pic from a php script and if there is no picture location it displays "null" for the location in the JSON output. This is throwing an exception in my code. Here is the code which is not working:

    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;
}

The error is : Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSNull length]: unrecognized selector sent to instance 0x98d5a0' Any suggestions?


回答1:


you need to correct your code a little bit.

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

i think this will do it for you.



来源:https://stackoverflow.com/questions/8785124/exception-if-nil-or-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!