Frankd
Frankd

Reputation: 17

NSString comparison not working in Objective-C

I have an issue with my NSString comparison method 'isEqualToString:'. I have two (matching) strings that do not come back as YES when executing hte isEqualToString: method.

The code within the if statement is never executed. I also tried adding a \n to the checkvalue string in case there was a hidden control code in the text string. At the bottom of the code are the variables showing in the debug window.

NSString *text = [dict objectForKey:TEXT_STR];

NSLog(@"%@", [text dataUsingEncoding:NSUTF8StringEncoding]);

NSString *checkvalue = @”OVERTEMP”;

NSLog(@"%@", [checkvalue dataUsingEncoding:NSUTF8StringEncoding]);

if ( [text isEqualtoString:checkvalue] )

{
    // ... execute this code when the strings are equal
}

Here are the variable two values shown in the Debugger Window, followed by the string from the NSLog statement:

text=(…NSCFString *) @”OVERTEMP”

checkvalue=(…NSCFContstantString *) @”OVERTEMP”

NSLog output for text:  <4f564552 54454d50 00000000 00>

NSLog output for checkvalue:  <4f564552 54454d50>

Upvotes: 0

Views: 212

Answers (1)

rob mayoff
rob mayoff

Reputation: 385600

Your text has some trailing NULs. You could trim them:

text = [text stringByTrimmingCharactersInSet:
    [NSCharacterSet characterSetWithRange:NSMakeRange(0, 1)]];

Upvotes: 1

Related Questions