Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the index of an object in an NSArray using string value?

I want to get the index of an object within the NSMutableArray of categories.

The category object has an attribute "category_title" and I want to be able to get the index by passing the value of category_title.

I have looked through the docs and can't find a simple way to go about this.

like image 272
jroyce Avatar asked Feb 03 '12 23:02

jroyce


People also ask

What is difference between NSArray and NSMutableArray?

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.

What is NS array?

An object representing a static ordered collection, for use instead of an Array constant in cases that require reference semantics.

Can NSArray contain nil?

arrays can't contain nil.

Is NSArray ordered?

The answer is yes, the order of the elements of an array will be maintained - because an array is an ordered collection of items, just like a string is an ordered sequence of characters...


2 Answers

NSArray does not guarantee that you can only store one copy of a given object, so you have to make sure that you handle that yourself (or use NSOrderedSet).

That said, there are a couple approaches here. If your category objects implement isEqual: to match category_title, then you can just use -indexOfObject:.

If you can't do that (because the category objects use a different definition of equality), use -indexOfObjectPassingTest:. It takes a block in which you can do whatever test you want to define your "test" - in this case, testing category_title string equality.

Note that these are all declared for NSArray, so you won't see them if you are only looking at the NSMutableArray header/documentation.

EDIT: Code sample. This assumes objects of class CASCategory with an NSString property categoryTitle (I can't bring myself to put underscores in an ivar name :-):

    CASCategory *cat1 = [[CASCategory alloc] init];
    [cat1 setCategoryTitle:@"foo"];
    CASCategory *cat2 = [[CASCategory alloc] init];
    [cat2 setCategoryTitle:@"bar"];
    CASCategory *cat3 = [[CASCategory alloc] init];
    [cat3 setCategoryTitle:@"baz"];

    NSMutableArray *array = [NSMutableArray arrayWithObjects:cat1, cat2, cat3, nil];
    [cat1 release];
    [cat2 release];
    [cat3 release];

    NSUInteger barIndex = [array indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
        if ([[(CASCategory *)obj categoryTitle] isEqualToString:@"bar"]) {
            *stop = YES;
            return YES;
        }
        return NO;
    }];

    if (barIndex != NSNotFound) {
        NSLog(@"The title of category at index %lu is %@", barIndex, [[array objectAtIndex:barIndex] categoryTitle]);
    }
    else {
        NSLog(@"Not found");
    }
like image 167
Conrad Shultz Avatar answered Oct 07 '22 09:10

Conrad Shultz


Not sure that I understand the question but something like this might work (assuming the Mutable Array contains objects of Class "Category"):

int indx;
bool chk;
for (Category *aCategory in theArray) 
{
    chk = ([[aCategory category_title] isEqualToString:@"valOfCategoryTitle"])
    if ( chk )
        indx = [theArray indexOfObject:aCategory];
}
like image 36
Bruce Dean Avatar answered Oct 07 '22 07:10

Bruce Dean