-2

I'm new to iOS development. I get an error when I compare NSString with NSString nil value. It is not working in if condition.

my code is:

  NSDictionary *responseFromJSON = [JSON objectForKey:@"response"];
  NSString *strResponseMsg = [responseFromJSON objectForKey:@"104"];   
  if ([strResponseMsg isEqualToString:nil])
     {
         NSLog(@"login Invalid");
     }
     else
     {
         NSLog(@"login success");
     }
Novarg
  • 7,390
  • 3
  • 38
  • 74

4 Answers4

1

You can simply do like this,

    NSString * string = nil;
if (string!=nil &&  // not nil, means 0x0 object
    string.length>0 && // at leaset one character should exists
    [string isEqual:[NSNull null]]) { // to avoid 'null' in string

    // valid string
}
Jay
  • 856
  • 7
  • 17
0

You can do like this,

if ([yourString isEqual:[NSNull null]])
{
  //your code goes here
}

Hope this helps.

iOSNoob
  • 1,420
  • 2
  • 16
  • 30
0

Use below

NSString *strResponseMsg = [responseFromJSON objectForKey:@"104"];   
if (!strResponseMsg)
{
    NSLog(@"login Invalid");
}
else
{
    NSLog(@"login success");
}
Tapas Pal
  • 7,073
  • 8
  • 39
  • 86
0

If you want to check the NSString whether it's nil or empty. You just need to do something like that:

if (strResponseMsg.length) {}

Because it will not go inside if the strResponseMsg is nil or empty. It will only go inside when strResponseMsg is not nil and no empty.

Lucas Huang
  • 3,998
  • 3
  • 20
  • 29