问题
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