比较 NSString 和 NSArray 对象时遇到问题
好的,所以我尝试检查 NSArray
中的对象是否等于用户输入到 UITextField
中的内容。它应该有效,但由于某种原因它不起作用。这是我的代码:
if (theAnswer.text == [correctAnswers objectAtIndex:problemNumber]) {
NSLog(@"CORRECT");
}
else {
NSLog(@"wrong");
}
控制台总是给出错误。 我输入此日志:
NSLog(@"%@ %@", theAnswer.text, [correctAnswers objectAtIndex:problemNumber]);
然后我得到 A A
wrong
每次打印。感谢您的帮助
Ok, so I'm trying to check if an object from an NSArray
equals something inputed by the User into a UITextField
. It should work, but for some reason it dosn't. Here is my code:
if (theAnswer.text == [correctAnswers objectAtIndex:problemNumber]) {
NSLog(@"CORRECT");
}
else {
NSLog(@"wrong");
}
The console always give wrong.
I put this log in:
NSLog(@"%@ %@", theAnswer.text, [correctAnswers objectAtIndex:problemNumber]);
And I get A A
wrong
printed everytime. Thanks for the help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Objective-C 不支持 NSString 的 == 运算符。这将比较 NSString 的指针,而不是字符串本身的内容。
尝试
Objective-C doesn't support the == operator for NSStrings. That will do a comparison of the pointers to the NSStrings and not the contents of the string itself.
Try