Search code examples
iosobjective-ccocoacase-sensitive

case insensitivity in NSArray containsObject:


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)


Solution

  • 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.