为什么我的String比较在iOS 6中不起作用? [英] Why isn't my String comparison working in iOS 6?

查看:96
本文介绍了为什么我的String比较在iOS 6中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

了解Objective-C中的NSString比较

I已经将这个简单的代码用于应用程序的多个版本,直到iOS 6字符串比较已经有效,但现在它失败了 - 为什么?

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


推荐答案

因为它从未真正起作用。使用 == 运算符比较字符串不起作用,因为字符串(NSString对象)是指针 - 进行数字比较只比较它们的地址,而不是它们的内容。你需要写

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
}

编辑:我听到你尖叫但它确实有效!它真的有效,直到iOS 6 ! - 不。它没有,这只是偶然的事情。

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

这篇关于为什么我的String比较在iOS 6中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆