1

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 ?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

And for the other half of the question: if the elements of the array are themselves mutable, you can just change it. For example, if the elements are mutable dictionaries, you could just invoke [[myArray objectAtIndex:idx] setObject:newObject forKey:someKey]. If the elements are immutable, then you can replace one with [myArray replaceObjectAtIndex:idx withObject:newObject].
Thanks for your answers guys. One last thing how do i change it so that I am no longer looking for a Boolean but say im looking for objects that have field "A" equal to a string say "test"?
use: return [[obj valueForKey:@"A"] isEqualToString:@"test"];
excellent thanks. When updating the element at a specific key how do I update it if the type is Boolean ( my array is mutable so i can do this i think as you have said) I suspect it is something along the lines of [[[myarray objectAtIndex:indx] setObject:NSNumber numberWithBool:YES forKey:@"yesorno"]; ???????

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.