2015-04-22 59 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:@“”]) { //字符串为空 } –