case insensitivity in NSArray containsObject:

thekmc picture thekmc · Nov 15, 2011 · Viewed 7.1k times · Source

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)

Answer

Lily Ballard picture Lily Ballard · Nov 15, 2011
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
}