如何将 UILable 文本(链接到 UIPickerView)与 NSString 进行比较?
所以我有一个 UILabel
链接到 UIPickerView
的当前选定值。
我还有一个按钮,按下该按钮时会调用一个方法,该方法从该 UILabel
读取值,将其与指定的 NSString
进行比较,并根据该比较来执行或不执行一个动作。
if (myLabel.text == @"sample text") {
// code that does something
}
然而,即使标签文本和我比较的文本看起来相同,它也没有通过验证。
我希望这已经足够清楚了。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
字符串比较
String comparison
在 Objective-C 中,你不能真正直接比较字符串指针,它几乎永远不会起作用。相反,您需要使用 isEqualToString:
NSString
方法,如下所示:有关处理字符串的更多信息,具体比较它们,请参阅 在 href="http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Strings/introStrings.html#//apple_ref/doc/uid/10000035-SW1" rel="nofollow">字符串编程指南。
In Objective-C, you can't really directly compare string pointers, it almost never works. Instead, you need to use the isEqualToString: method of
NSString
, like this:For more on dealing with strings, specifically comparing them, see Searching, Sorting, and Comparing Strings in the String Programming Guide.