user2813740 user2813740 - 1 year ago 68
Objective-C Question

NSDictionary checking for "<null>" value in ios10

i have NSMutableDictionary which stores data comping from database.
some keys inside NSMutableDictionary has null values like the one shown below.
i wanted to check for null values, i am doing as below

UserMobileNo = "<null>";
UserAccNo = "<null>";
if(!([dicUserdata valueForKey:@"UserMobileNo"] == [NSNull null]))
//Saved result

but above code do not seems to be checking for null values perfectly.
what is the perfect way of checking null values inside dictionary?

Answer Source

There are many ways to check.

Using nil:

if([dicUserdata objectForKey:@"UserMobileNo"] == nil){
    // found null

Using isEqual method:

if([[dicUserdata objectForKey:@"UserMobileNo"] isEqual:[NSNull null]]){
    // found null

isEqual:Returns a Boolean value that indicates whether the receiver and a given object are equal.

Using == operator:

if[[dicUserdata objectForKey:@"UserMobileNo"] == [NSNull null]]){
    // found null

Using isKindOfClass method:

if[[dicUserdata objectForKey:@"UserMobileNo"] isKindOfClass:[NSNull null]]){
    // found null

isKindOfClass: Returns a Boolean value that indicates whether the receiver is an instance of given class or an instance of any class that inherits from that class.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download