1

I have a custom class extending NSObject. I am maintaining NSMutableArray of this class objects. Here is the situation,

customObject-class {
   NSString *name;
   int ID;
   .....and many other properties;
}

customObjectsArray [
   customObject1,
   customObject2,
   ...etc
]

Now I am trying to use filterUsingPredicate to remove objects that has nil names, like below but it returns very few or none objects while I know that there are hundreds of objects that has name not nil or empty. Could someone please tell me what could be wrong here.

[customObjectsArray filterUsingPredicate:[NSPredicate predicateWithFormat:@"name != nil"]];
3
  • I presume you actually have the final quotation mark that's missing in your predicate. What you posted wouldn't even compile. Commented Dec 16, 2012 at 23:08
  • Other than that missing quote mark, I don't see anything wrong with this. Are you sure that there are hundreds of objects with non-nil names? Commented Dec 16, 2012 at 23:26
  • it was copy-paste error...corrected it. Yah there were hundreds of objects coming from server. I found an issue. This predicate was getting called before customObject1's data were actually initialised. I should check the status of data flag that says data has been initialised for this particular object and then apply filter. It worked. If data is not initialised all object's name is off course nil! Commented Dec 17, 2012 at 0:14

2 Answers 2

5

Why won't you try like this:

NSMutableArray *array=...;
[array filterUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
    CustomObject *customObject=(CustomObject *) evaluatedObject;
    return (customObject.name!=nil);
}]];
Sign up to request clarification or add additional context in comments.

1 Comment

the perfect general example of filterUsingPredicate.
0

As I replied to @rdelmar, I found an issue. This predicate was getting called before customObject1's data were actually initialised. I should check the status of data flag that says data has been initialised for this particular object and then apply filter. It worked. If data is not initialised all object's name is off course nil!

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.