PDA

View Full Version : compare string to null




tranvutuan
Feb 9, 2012, 08:29 AM
After parsing and get values for each elements from XML, one of the field is null like below

myXML.h
@property (strong, nonatomic) NSString *address;
@property (strong, nonatomic) NSString *phone;

Below is my xml structure
<myXML>
<address> A Street </address>
<phone> (null) </phone>
</myXML>

I am doing a checking :
if ( ![myMXL.phone isEqualToString:@"null"] ) {
NSLog(@"phone is not null");
else
NSLog(@"phone is null");

After running the codes, the debugger gives me
phone is not null

It seems isEqualToString is not working.I also try !(myXML.phone == NULL) and !([myXML.phone isEqual:NULL]), but still get same problem
Does anyone have an idea about this.. pls advice me. Thanks



tranvutuan
Feb 9, 2012, 08:43 AM
After parsing and get values for each elements from XML, one of the field is null like below

myXML.h
@property (strong, nonatomic) NSString *address;
@property (strong, nonatomic) NSString *phone;

Below is my xml structure
<myXML>
<address> A Street </address>
<phone> (null) </phone>
</myXML>

I am doing a checking :
if ( ![myMXL.phone isEqualToString:@"null"] ) {
NSLog(@"phone is not null");
else
NSLog(@"phone is null");

After running the codes, the debugger gives me
phone is not null

It seems isEqualToString is not working.I also try !(myXML.phone == NULL) and !([myXML.phone isEqual:NULL]), but still get same problem
Does anyone have an idea about this.. pls advice me. Thanks

I've just found the way how to solve this issue. Because phone is NNString so we can count the length of it. Therefore, instead of comparing to null, I did
if (!([phone length] == 0 ))
// do sth here
But I am still looking for other solutions, so please answer if you have another solution. Thanks

dantastic
Feb 9, 2012, 10:25 AM
(null) is not a string, it's nothing. you can't compare it to the string "null"

if you want to check for null do

if (myMXL.phone) {
NSLog(@"phone has a value: %@", myMXL.phone);
} else {
NSLog(@"this will print (null): %@", myMXL.phone);
}

tranvutuan
Feb 9, 2012, 10:53 AM
(null) is not a string, it's nothing. you can't compare it to the string "null"

if you want to check for null do

if (myMXL.phone) {
NSLog(@"phone has a value: %@", myMXL.phone);
} else {
NSLog(@"this will print (null): %@", myMXL.phone);
}

I have just gave it a try but it does not work... What I got from the console is
phone has a value :(null)
I dont know why because we already know from OP, my phone is null

dantastic
Feb 9, 2012, 11:12 AM
odd..
Or do you actually have a string with "(null)" in it?
If you add


if (myMXL.phone) {
NSLog(@"phone has a value: >%@< of Kind: %@, length: %i", myMXL.phone, [myMXL.phone class], [myMXL.phone length]);
} else {
NSLog(@"this will print (null): %@", myMXL.phone);
}