如何将 UILable 文本(链接到 UIPickerView)与 NSString 进行比较?

发布于 2024-12-21 20:05:50 字数 344 浏览 2 评论 0 原文

所以我有一个 UILabel 链接到 UIPickerView 的当前选定值。

我还有一个按钮,按下该按钮时会调用一个方法,该方法从该 UILabel 读取值,将其与指定的 NSString 进行比较,并根据该比较来执行或不执行一个动作。

if (myLabel.text == @"sample text") {
  // code that does something 
}

然而,即使标签文本和我比较的文本看起来相同,它也没有通过验证。

我希望这已经足够清楚了。

So I have a UILabel that is linked to the current selected value of a UIPickerView.

I also have a button that when pressed calls a method that reads the value from that UILabel, compares it with a specified NSString and according to that comparison and does or does not perform an action.

if (myLabel.text == @"sample text") {
  // code that does something 
}

However even though the label text and the text that I compare it to seem identical, it doesn't pass the verification.

I hope that was clear enough.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

弃爱 2024-12-28 20:05:50

字符串比较

if([myLabel.text isEqualToString:@"sample text"])
{
//code
}

String comparison

if([myLabel.text isEqualToString:@"sample text"])
{
//code
}
爱殇璃 2024-12-28 20:05:50

在 Objective-C 中,你不能真正直接比较字符串指针,它几乎永远不会起作用。相反,您需要使用 isEqualToString: NSString 方法,如下所示:

if( [myLabel.text isEqualToString:@"sample text"] ) {
  // do something
}

有关处理字符串的更多信息,具体比较它们,请参阅 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:

if( [myLabel.text isEqualToString:@"sample text"] ) {
  // do something
}

For more on dealing with strings, specifically comparing them, see Searching, Sorting, and Comparing Strings in the String Programming Guide.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文