I have a NSMutableArray
which contains a few NSString
objects. How can I test if the array contains a particular string literal?
I tried [array containsObject:@"teststring"]
but that doesn't work.
The primary difference between NSArray and NSMutableArray is that a mutable array can be changed/modified after it has been allocated and initialized, whereas an immutable array, NSArray , cannot.
In general, the collection classes (for example, NSMutableArray , NSMutableDictionary ) are not thread-safe when mutations are concerned. That is, if one or more threads are changing the same array, problems can occur.
The NSMutableArray class declares the programmatic interface to objects that manage a modifiable array of objects. This class adds insertion and deletion operations to the basic array-handling behavior inherited from NSArray . NSMutableArray is “toll-free bridged” with its Core Foundation counterpart, CFMutableArray .
What you're doing should work fine. For example
NSArray *a = [NSArray arrayWithObjects:@"Foo", @"Bar", @"Baz", nil]; NSLog(@"At index %i", [a indexOfObject:@"Bar"]);
Correctly logs "At index 1" for me. Two possible foibles:
indexOfObject
sends isEqual
messages to do the comparison - you've not replaced this method in a category?NSNotFound
for failure to locate, and not (say) 0.[array indexOfObject:object] != NSNotFound
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With