17

I have an object from a NSObject class that I call "brand" and has the following properties:

.name
.number
.site

at a given time, dozens of these objects are stored on a NSMutableArray, something like:

object 1
object 2
object 3
object 4
...

I would like to be able to retrieve a given object from the array by its number. Not the index of the object on the array but the number property of the object (get object that has the number property equal to 10, for example).

I know that NSArrays have smart methods for retrieving stuff but I don't know them deeply cause I use this rarely. Is there any way to retrieve that object from the array without having to iterate thru all objects on the array and check each object's number property?

Can you guys help? thanks.

4 Answers 4

15

I would recommend NSPredicate. You could do it with something like this, assuming listOfItems is your array containing your NSObject's with said properties.

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"number == 10"];
NSArray *filtered = [listOfItems filteredArrayUsingPredicate:predicate];

Your filtered results, any numbers matching 10 will now be in the filtered array. If you want to cast back to your object, you can do something like this:

YourObject *object = (YourObject*)[filtered objectAtIndex:0];

Hope this helps.

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

Comments

12
NSUInteger indexOfObject10 = [myArray indexOfObjectPassingTest:^(id obj, NSUInteger idx, BOOL *stop){
    MyObject *object = (MyObject *)obj;
    return [object.number == 10];
}];

MyObject *object10 = myArray[indexOfObject10];

Comments

1

I prefer using block extensions to NSArray that let you "talk" to them more naturally and write less code. Something in the lines of the Smalltalk Collections jargon:

  • detect:
  • select:
  • inject:into:
  • etc.

In your case I would use:

MyObject* theObject = [myArray detect:^(MyObject* each){
     return each.number == 10;
}];

Here you have a possible implementation for that extension to NSArray

typedef BOOL (^BlockPredicate) (id element);

-(id) detect:(BlockPredicate) predicateBlock{
     for (id each in self) {
        if (predicateBlock(each)) {
            return each;
        }
    }
    return nil;
}

I find this kind of tools really helpful. I hope you find them useful too.

2 Comments

what happens if the array contains more than one object with the same number?
This implementation will 'detect' and return the first one. If you wish to find all the instances with the same number, you could use select:.
0

Maybe something like this:

NSObject *retrieveObject = [[NSObject alloc] init];
retrieveObject.number = 1;

NSObject *anObject = [yourMutableArray objectAtIndex:[yourMutableArray indexOfObjectIdenticalTo:testObject]];

First, I create an object with the same number as an object in the array that I want to retrieve (in this case "1"). In the next line, I assume that when you retrieve it you want to assign it's value to another object, so I retrieve it by using the standard objectAtIndex: method with the index parameter being another method, the indexOfObjectIdenticalTo: method, with that method's parameter being the retriveObject object. So what this does is it looks in the array for the first object identical to retrieveObject (meaning they'd have the same number) and returns that object's index, and then passes that value into the objectAtIndex method.

Hope this helps!

Comments

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.