NSArray containsObject 中不区分大小写:

发布于 2024-12-15 14:56:40 字数 369 浏览 3 评论 0原文

我正在开发一个应用程序,该应用程序检查数组以查看它是否包含特定字符串。但是,它似乎工作不正常,因为我认为它可能正在查看字符串的大小写。 在这行代码中,我如何确保 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 技术交流群。

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

发布评论

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

评论(5

怀中猫帐中妖 2024-12-22 14:56:40
if ([myArray indexOfObjectPassingTest:^(id obj, NSUInteger idx, BOOL *stop){
         return (BOOL)([obj caseInsensitiveCompare:term] == NSOrderedSame);
     }] != NSNotFound) {
    // there's at least one object that matches term case-insensitively
}
if ([myArray indexOfObjectPassingTest:^(id obj, NSUInteger idx, BOOL *stop){
         return (BOOL)([obj caseInsensitiveCompare:term] == NSOrderedSame);
     }] != NSNotFound) {
    // there's at least one object that matches term case-insensitively
}
山田美奈子 2024-12-22 14:56:40

这是使用键值编码的有效方法。

NSArray *strings; // array of strings
if ([[strings valueForKey:@"lowercaseString"] containsObject:[searchString lowercaseString]]) {
   NSLog(@"searchString found");
} else {
   NSLog(@"searchString NOT found");
}

斯威夫特 3 选项:

let things = ["foo", "Bar"]
let searchString = "FOO"

if things.contains(where: { $0.lowercased() == searchString.lowercased() }) {
    print("found searchString")
} else {
    print("searchString not found")
}

Here's an effective approach using key-value coding.

NSArray *strings; // array of strings
if ([[strings valueForKey:@"lowercaseString"] containsObject:[searchString lowercaseString]]) {
   NSLog(@"searchString found");
} else {
   NSLog(@"searchString NOT found");
}

Swift 3 Option:

let things = ["foo", "Bar"]
let searchString = "FOO"

if things.contains(where: { $0.lowercased() == searchString.lowercased() }) {
    print("found searchString")
} else {
    print("searchString not found")
}
_失温 2024-12-22 14:56:40

NSArray (故意)不知道它包含什么。因此,它不知道 NSString,更不用说区分大小写了。您必须循环遍历数组的项目并比较它们。您可以使用多种不同方法之一来循环数组:

  • objectEnumerator
  • indexOfObjectPassingTest:,它使用块来执行操作。
  • 为每个项目调用objectAtIndex:

我建议选择前两个选项之一。

NSArray is (purposefully) ignorant of what it holds. Thus, it isn't aware of NSString, 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.
  • call objectAtIndex: for each item.

I would suggest one of the first 2 options.

清欢 2024-12-22 14:56:40
- (BOOL)containsObject:(id)anObject 

是一种方便的方法,您无法将其用于您的目的。这是文档讨论:

此方法通过以下方式确定数组中是否存在对象
向数组的每个对象发送 isEqual: 消息(以及
将 anObject 作为参数传递给每个 isEqual: 消息)。

一般的 NSObject 方法 isEqual: 方法无法考虑字符串字符的大小写。如果您可以使用块(iOS 4),最好的解决方案可能类似于 Kevin Ballard 的提议。如果没有,那么您可能必须遍历字符串。

- (BOOL)containsObject:(id)anObject 

is a convenience method which you cannot apply for your purpose. Here is the documentation discussion:

This method determines whether anObject is present in the array by
sending an isEqual: message to each of the array’s objects (and
passing anObject as the parameter to each isEqual: message).

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.

兔姬 2024-12-22 14:56:40
if ([@"Some String" caseInsensitiveCompare:@"some string"] == NSOrderedSame ) {
    //...
}

这个可能对你有用......祝你一切顺利

if ([@"Some String" caseInsensitiveCompare:@"some string"] == NSOrderedSame ) {
    //...
}

this one may be useful to you... All The best

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