2012-01-09 73 views
0

Im從php腳本中拉出圖片的位置,如果沒有圖片位置,它會在JSON輸出中顯示位置「null」。這是在我的代碼中拋出異常。這裏是一個不工作的代碼:異常如果沒有或<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長度]:無法識別的選擇發送到實例0x98d5a0」 有什麼建議麼?

回答

9

您需要稍微糾正一下您的代碼。

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

我認爲這會爲你做。

+0

確實可以接受。 – 2012-01-09 07:43:18

+0

謝謝先生。一起來:) – savagenoob 2012-01-09 07:45:40

+0

作品完美賽亞! – Nirav 2013-05-07 05:20:01