Checking a null value in Objective-C that has been returned from a JSON string

前端 未结 15 1409
北海茫月
北海茫月 2020-11-30 20:34

I have a JSON object that is coming from a webserver.

The log is something like this:

{          
   \"status\":\"success\",
   \"Us         


        
相关标签:
15条回答
  • 2020-11-30 21:15
    if([tel isEqual:[NSNull null]])
    {
       //do something when value is null
    }
    
    0 讨论(0)
  • 2020-11-30 21:15

    Here you can also do that by checking the length of the string i.e.

    if(tel.length==0)
    {
        //do some logic here
    }
    
    0 讨论(0)
  • 2020-11-30 21:16

    Try this:

    if (tel == (NSString *)[NSNull null] || tel.length==0)
    {
        // do logic here
    }
    
    0 讨论(0)
  • 2020-11-30 21:18

    I use this:

    #define NULL_TO_NIL(obj) ({ __typeof__ (obj) __obj = (obj); __obj == [NSNull null] ? nil : obj; }) 
    
    0 讨论(0)
  • 2020-11-30 21:21

    if we are getting null value like then we can check it with below code snippet.

     if(![[dictTripData objectForKey:@"mob_no"] isKindOfClass:[NSNull class]])
          strPsngrMobileNo = [dictTripData objectForKey:@"mobile_number"];
      else
               strPsngrMobileNo = @"";
    
    0 讨论(0)
  • 2020-11-30 21:22

    I usually do it like this:

    Assuma I have a data model for the user, and it has an NSString property called email, fetched from a JSON dict. If the email field is used inside the application, converting it to empty string prevents possible crashes:

    - (id)initWithJSONDictionary:(NSDictionary *)dictionary{
    
        //Initializer, other properties etc...
    
        id usersmail = [[dictionary objectForKey:@"email"] copy];
        _email = ( usersmail && usersmail != (id)[NSNull null] )? [usersmail copy] : [[NSString      alloc]initWithString:@""];
    }
    
    0 讨论(0)
提交回复
热议问题