I am develop a iPhone application, in which i need to use JSON
to receive data from server.
In the iPhone side, I convert the data into NSMutableDictionary
.
However, there is a date type data are null.
I use the following sentence to read the date.
NSString *arriveTime = [taskDic objectForKey:@"arriveTime"];
NSLog(@"%@", arriveTime);
if (arriveTime) {
job.arriveDone = [NSDate dateWithTimeIntervalSince1970:[arriveTime intValue]/1000];
}
When the arriveTime is null, how can i make the if statement. I have tried [arriveTime length] != 0, but i doesn't work, because the arriveTime is a NSNull and doesn't have this method.
We can use NSNull to represent nil objects in collections, and we typically see NSNull when we convert JSON to Objective-C objects and the JSON contained null values. Our preferred way to check if something is NSNull is [[NSNull null] isEqual:something] .
So a good test might be: if (title == (id)[NSNull null] || title. length == 0 ) title = @"Something"; Save this answer.
Objective-C builds on C's representation of nothing by adding nil . nil is an object pointer to nothing. Although semantically distinct from NULL , they are technically equivalent.
A singleton object used to represent null values in collection objects that don't allow nil values.
the NSNull
instance is a singleton. you can use a simple pointer comparison to accomplish this:
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); }
alternatively, you could use isKindOfClass:
if you find that clearer:
if (arriveTime == nil) { NSLog(@"it's nil"); }
else if ([arriveTime isKindOfClass:[NSNull class]]) {
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With