Ales Ales - 1 year ago 111
iOS Question

NSMutableArray objectAtIndex

I have NSMutableArray "Tag" with objects on index 0: "data", "data1", "data2" on index 1: "data3" on index 2: "data2" ...
My chosen_tag is "data2". How to get index 0 and 2. I use Tableview to show my results in cell. NSLog show index 9223372036854775807

NSUInteger index = [Tag indexOfObject:chosen_tag];

My cell:

UILabel * cellLabel = (UILabel *)[cell viewWithTag:2];
[cellLabel setText:[Name objectAtIndex:index]];

If I NSLog my NSMutableArray Tag it show
( "data, data1, data2", "data3", "data2, data3, data4, data5, data6", "" )

Which means data2 is on index 0 and index 2. My problem is that I have "data2" on third place on index 0.

Answer Source

How to get index 0 and 2 ?

You can use the method indexesOfObjectsPassingTest it returns an NSIndexSet object:

NSArray *tag = @[@"data, data1, data2", @"data3", @"data2, data3, data4, data5, data6", @""];

NSIndexSet *indexSet = [tag indexesOfObjectsPassingTest:^BOOL(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    return [[(NSString *)obj componentsSeparatedByString:@", "] containsObject:@"data2"];

NSLog(@"%@", indexSet);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download