Testing if NSMutableArray contains a string object

后端 未结 5 489
醉酒成梦
醉酒成梦 2021-01-31 14:59

I have a NSMutableArray which contains a few NSString objects. How can I test if the array contains a particular string literal?

I tried

5条回答
  •  面向向阳花
    2021-01-31 15:42

    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:

    1. indexOfObject sends isEqual messages to do the comparison - you've not replaced this method in a category?
    2. Make sure you're testing against NSNotFound for failure to locate, and not (say) 0.

提交回复
热议问题