繁体   English   中英

如何在objective-c中使用NSNull创建if语句

[英]How to make a if statement with NSNull in objective-c

我正在开发一个iPhone应用程序,我需要使用JSON从服务器接收数据。 在iPhone方面,我将数据转换为NSMutableDictionary

但是,日期类型数据为空。

我用下面的句子来读日期。

NSString *arriveTime = [taskDic objectForKey:@"arriveTime"];
NSLog(@"%@", arriveTime);

if (arriveTime) {
    job.arriveDone = [NSDate dateWithTimeIntervalSince1970:[arriveTime intValue]/1000];
}

当arrivalTime为null时,如何创建if语句。 我试过[到达时间长度]!= 0,但我不工作,因为到达时间是NSNull并且没有这个方法。

NSNull实例是单例。 您可以使用简单的指针比较来完成此任务:

if (arriveTime == nil) { NSLog(@"it's nil"); }
else if (arriveTime == (id)[NSNull null]) { // << the magic bit!
  NSLog(@"it's NSNull");
}
else { NSLog(@"it's %@", arriveTime); }

或者,你可以使用isKindOfClass:如果你发现更清楚:

if (arriveTime == nil) { NSLog(@"it's nil"); }
else if ([arriveTime isKindOfClass:[NSNull class]]) {
  ...

在一条线上

arriveTime ? job.arriveDone = [NSDate dateWithTimeIntervalSince1970:[arriveTime intValue]/1000]; : NSLog(@"Arrive time is not yet scheduled");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM