views:

36

answers:

1

Hi,

I'm trying to compare two strings with each other on the iphone, but the IF statement is always false. Here's my code:

NSLog([[UIDevice currentDevice]uniqueIdentifier]);
if ([[[UIDevice currentDevice]uniqueIdentifier] compare:[[NSUserDefaults standardUserDefaults] valueForKey:@"id"]] == NSOrderedSame) {
    //OK
    NSLog("Do stuff if equal");
} else {
    // Not OK
    NSLog("Do stuff if not equal");
}

The output is always

*** UDID *** (<- my device's UDID)
Do stuff if not equal

What am I doing wrong?

Thanks! Yvan

+2  A: 

Try using stringForKey: instead of valueForKey:, and isEqualToString: instead of compare: ... == NSOrderedSame.

Douwe Maan
This solved the problem! Thanks!
Yvan JANSSENS
Using isEqualToString: instead of compare: is unlikely to fix things; the main difference is that compare: does a (non-literal) Unicode string comparison which is slower but "correct".
tc.
I know, the fix was in changing `valueForKey:` to `stringForKey:`, the `isEqualToString:` instead of `compare:` was just because I think it's better/easier to use that in this case.
Douwe Maan