NSArray containsObject 中不区分大小写:
我正在开发一个应用程序,该应用程序检查数组以查看它是否包含特定字符串。但是,它似乎工作不正常,因为我认为它可能正在查看字符串的大小写。 在这行代码中,我如何确保 containsObject: 不区分大小写?
if ([myArray containsObject:term]) {...}
请询问您是否需要澄清,并感谢您的帮助。
(另外,我发现了这个问题:不区分大小写的比较 NSString。我不知道如果这是我需要的,如果是,我将如何使用它)
I'm working on an app that checks an array to see if it contains a certain string. However, it doesn't seem to be working right, because I think it may be looking at the case of the string.
In this line of code, how would I make sure that containsObject: is being case insensitive?
if ([myArray containsObject:term]) {...}
Please ask if you need clarification, and thanks for your help.
(Also, I've found this question: Case insensitive comparison NSString. I don't know if this is what I need, and if it is, how would I use it)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是使用键值编码的有效方法。
斯威夫特 3 选项:
Here's an effective approach using key-value coding.
Swift 3 Option:
NSArray
(故意)不知道它包含什么。因此,它不知道NSString
,更不用说区分大小写了。您必须循环遍历数组的项目并比较它们。您可以使用多种不同方法之一来循环数组:objectEnumerator
indexOfObjectPassingTest:
,它使用块来执行操作。objectAtIndex:
。我建议选择前两个选项之一。
NSArray
is (purposefully) ignorant of what it holds. Thus, it isn't aware ofNSString
, much less about case sensitivity. You'll have to loop through the items of the array and compare them. You can use one of many different ways of looping through the array :objectEnumerator
indexOfObjectPassingTest:
which uses blocks to perform your operation.objectAtIndex:
for each item.I would suggest one of the first 2 options.
是一种方便的方法,您无法将其用于您的目的。这是文档讨论:
一般的 NSObject 方法
isEqual:
方法无法考虑字符串字符的大小写。如果您可以使用块(iOS 4),最好的解决方案可能类似于 Kevin Ballard 的提议。如果没有,那么您可能必须遍历字符串。is a convenience method which you cannot apply for your purpose. Here is the documentation discussion:
The general NSObject method
isEqual:
method cannot be made to take into consideration the case of string characters. The best solution if you can use blocks (iOS 4) is probably something like Kevin Ballard's proposition. If not then you might have to iterate through the strings.这个可能对你有用......祝你一切顺利
this one may be useful to you... All The best