[英]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.