-3

Possible Duplicate:
Understanding NSString comparison in Objective-C

I've used this simple code for several releases of an app, and until iOS 6 the string comparison has worked but now it fails -Why?

if(selectedCell.textLabel.text==@"Font"){
    NSLog(@"going to dofontpicker");
    [self doFontPicker];
}else if(selectedCell.textLabel.text==@"Color"){
    NSLog(@"going to do colorpicker");
    [self doColorPicker];
}
Community
  • 1
  • 1
wayneh
  • 4,393
  • 9
  • 35
  • 70

1 Answers1

10

Because it never really worked. Comparing strings doesn't work using the == operator, since strings (NSString objects) are pointers - doing a numerical comparison only compares their address, not their contents. You need to write

if ([someString isEqualToString:@"Font"]) {
    // do stuff
}

Edit: I hear you screaming "But it worked! It really worked until iOS 6!" - Nope. It didn't, it was just something accidental.

  • 2
    You're right - I'm an idiot and I do it correctly in other places...thanks – wayneh Oct 06 '12 at 18:15
  • @wayneh, lol its nice that you admitted. Now delete this question before your question rating goes further in free fall. – Sam B Oct 07 '12 at 12:56