簡體   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