viral
viral

Reputation: 4208

Condition for checking NOT <null> value for NSString in Objective C

Code:

NSString *tempPhone = [NSString stringWithFormat:@"%@", [personDict objectForKey:kPhoneKey]];
NSLog(@"NSString *tempPhone = %@",tempPhone);

Output:

NSString *tempPhone = <null>

Now I want to add if condition, for not null; I tried followings:

if (tempEmail != NULL)
if (tempPhone != Nil)
if (tempPhone != nil)
if (![tempPhone compare:@"<null>"])
if (tempPhone != (NSString*)[NSNull null])

I also Checked this Post.

None of them is working for me. Where I am going wrong??

Upvotes: 2

Views: 9273

Answers (9)

Fansad SoftFruit
Fansad SoftFruit

Reputation: 11

if ([str_name isKindOfClass:[NSNull class]])

works for me...

Upvotes: 1

Mutablegopi
Mutablegopi

Reputation: 281

First we need to check string length.

if (tempPhone.length == 0) `

{

//Your String is having empty value (E.x, (null))

}

else

{

// You having some values in this string

}`

Upvotes: 0

Devaski
Devaski

Reputation: 431

In case the string is having a null value. Example in NSLog.. Implement this way..

  if ([StringName isKindOfClass:[NSNULL Class]]) {

   }
   else {

   }

Upvotes: 0

lxcid
lxcid

Reputation: 1043

Because compare method returns type NSComparisonResult which is defined as

enum _NSComparisonResult {NSOrderedAscending = -1, NSOrderedSame, NSOrderedDescending};
typedef NSInteger NSComparisonResult;

If the string is the same, it will return NSOrderedSame which have a NSInteger value of 0.

Thus, the following line actually means...

if (![tempPhone compare:@"<null>"]) // `tempPhone` is equals to `@"<null>"`

or in a more understandable explanation, if tempPhone value is equal to @"<null>".

You should write it as

if ([tempPhone compare:@"<null>"] != NSOrderedSame)

or

if (![tempPhone isEqualString:@"<null>"])

Upvotes: 0

Stefan Ticu
Stefan Ticu

Reputation: 2103

It's like this :

if (![tempPhone isKindOfClass:[NSNull class]] && tempPhone != nil)

Upvotes: 1

Ilanchezhian
Ilanchezhian

Reputation: 17478

Try the following code

id phoneNo = [personDict objectForKey:kPhoneKey];

if( phoneNo && ![phoneNo isKindOfClass:[NSNull class]] )
{
    NSString *tempPhone = [NSString stringWithFormat:@"%@", [personDict objectForKey:kPhoneKey]];
    NSLog(@"NSString *tempPhone = %@",tempPhone);
}
else
{
    NSLog(@"NSString *tempPhone is null");
}

Upvotes: 8

viral
viral

Reputation: 4208

I Checked the webResponse (JSON, in my case). It was returning me the string with value:

<null>       

So, The following condition worked for me:

if (![tempPhone isEqualToString:@"<null>"])

Thanks for all your time. Thanks.

Upvotes: 0

Ganesh
Ganesh

Reputation: 1079

if (tempPhone != nil || [tempPhone isEqualToString:@"(null)"])

Works for me.

Upvotes: 1

rob mayoff
rob mayoff

Reputation: 385500

I think your personDict does not have an object for the key kPhoneKey, so it is returning nil. When you format nil using %@, you get the string "(null)".

id object = [personDict objectForKey:kPhoneKey];
if (object) {
    NSString *tempPhone = [NSString stringWithFormat:@"%@", object];
    NSLog(@"NSString *tempPhone = %@",tempPhone);
} else {
    // object is nil
}

Upvotes: 1

Related Questions