2015-04-22 66 views
3

j我正在構建一個應用程序,我希望在UIButton中顯示清潔人員的聯繫信息,如果清潔人員已分配給作業,並且不顯示該UIButton。我有「約會」對象具有更清潔和更乾淨的電話字符串作爲屬性。我試圖用它來檢查是否打印字符串:什麼是防彈方法來檢查NSString是否爲空/空?

if(appt.cleanerPhone) 

但是,這會打印出一個空的電話號碼,這是沒有幫助的按鈕。於是我試着

if([appt.cleanerPhone length]>2) 

,這是工作的昨天,但剛開始崩潰我的應用程序與此錯誤消息指着線以上:

2015-04-22 18:59:07.165 [640:158880] -[NSNull length]: unrecognized selector sent to instance 0x37afd690 

我也試過

if(appt.cleanerPhone && [appt.cleanerPhone length]>2) 

但這導致相同的錯誤。

僅供參考,這些全部來自解析的JSON對象 - > NSDictionary。如果沒有吸塵器,這裏是什麼的NSDictionary顯示的NSLog:

cleaner = "<null>"; 
    cleanerPhone = "<null>"; 

所以,我怎麼能安全穩健地檢查我的約會類的字符串對象非空?以上兩種方法有什麼問題?

+0

另外:http://stackoverflow.com/questions/5684157/how-to-detect-if-nsstring-is-null和http://stackoverflow.com/questions/19546920/how- to-check-nsstring-is-null-or-not –

回答

1

您從JSON或其他數據交換格式獲取數據。該轉換使null成爲[NSNull null]對象。在使用它之前,您需要輸入檢查值。嘗試:

if ([appt.cleanerPhone isKindOfClass:[NSString class]] && ([appt.cleanerPhone length] > 2)) {...} 
+1

爲了顯式檢查NSNull,與'=='與'[NSNull null]'進行比較。 –

+0

if([strpass isEqual:[NSNull null]] || strpass == nil || [strpass isEqualToString:@「」] || [strpass isEqualToString:@「(null)」] || strpass.length == 0 || [strpass isEqualToString:@「」]) { //字符串爲空 } –