user1096447
user1096447

Reputation: 429

change element value nsmutablearray

hi i have an array which holds 200 objects or so. Each of these objects is another array with 6 fields of mixed types ( ints, strings and bools).

2 questions...

can i search the array to find the objects that have a certain element i.e say all objects that have element "A" = TRUE.

How do i update a single element from one of the objects? DO i have to find that object (from the parent array hence why i asked the first question) , remove it then add a new object with the updated field? seems a bit overkill but is this what i need to do? is there anyway just to update that single element ?

Upvotes: 1

Views: 340

Answers (1)

rdelmar
rdelmar

Reputation: 104082

Yes you can search for that, and yes you must if you're going to change a value. You can use indexOfObjectPassingTest to find the object. in your posted example, you would use it like this (assuming that your objects are each dictionaries with one of the fields being "A"):

NSUInteger indx =[myArray indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
        return [[obj valueForKey:@"A"] isEqualToNumber:[NSNumber numberWithBool:TRUE]];
    }];

indx will be the index of the object that passes that test in your array.

Upvotes: 1

Related Questions