Why isn't my String comparison working in iOS 6? [duplicate]

∥☆過路亽.° 提交于 2019-12-02 23:39:22

问题


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];
}

回答1:


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.



来源:https://stackoverflow.com/questions/12762444/why-isnt-my-string-comparison-working-in-ios-6

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!