我已经在应用程序的几个版本中使用了这个简单的代码,直到iOS 6字符串比较已经有效,但现在它失败了 - 为什么?
if(selectedCell.textLabel.text==@"Font"){
NSLog(@"going to dofontpicker");
[self doFontPicker];
}else if(selectedCell.textLabel.text==@"Color"){
NSLog(@"going to do colorpicker");
[self doColorPicker];
}
Run Code Online (Sandbox Code Playgroud)
小智 10
因为它从未真正起作用.比较字符串不能使用==运算符,因为字符串(NSString对象)是指针 - 进行数字比较只比较它们的地址,而不是它们的内容.你需要写
if ([someString isEqualToString:@"Font"]) {
// do stuff
}
Run Code Online (Sandbox Code Playgroud)
编辑:我听到你尖叫"但它确实有效!它真的有效,直到iOS 6!" - 不.它没有,这只是偶然的事情.