1

I'm pretty new to NSPredicate, and sorry if this is newbie question, but I'v done my research and couldn't find answer to this.

I would like to filter array of custom objects by their function, not property.

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%@ LIKE %@", [times objectAtIndex:x] ,[unit realTime]];
NSArray *filtered  = [objectArray filteredArrayUsingPredicate:predicate];

the objectArray contains only unit Objects. And i would like to filter the array by each object in Array by [unit realTime] method result. Basically I'd like to have filtered array where [times objectAtIndex:x] == [unit realTime]. Is this possible to do?

5
  • Check if this works, [NSPredicate predicateWithFormat:@"%K LIKE %@", unit.realTime, [times objectAtIndex:x]]; Commented Jan 18, 2013 at 8:43
  • Sorry, nop. unit.realTime gets as undeclared identifier. Commented Jan 18, 2013 at 8:45
  • Why dont you declare realTime as an @property(readonly) in unit and then implement realTime method. Is that possible? Commented Jan 18, 2013 at 8:47
  • Hmm, sorry but I couldn't grasp the idea. realTime method is already implement in unit Class. Could you be more specific or give a simple example ? Commented Jan 18, 2013 at 8:53
  • 1
    You can add realTime as an @property() NSDate *realTime; in Unit.h file and then implement realTime method just like how you have already done. That way realTime is a param in class and you are overriding the getter method with your custom implementation. It shouldnt show an undeclared identifier error in this case. Commented Jan 18, 2013 at 8:57

1 Answer 1

1

You can add realTime as an @property() NSDate *realTime; in Unit.h file and then implement realTime method just like how you have already done. That way realTime is a param in class and you are overriding the getter method with your custom implementation. It shouldnt show an undeclared identifier error in this case.

Once you have done that, change the predicate statement as,

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K LIKE %@", unit.realTime, [times objectAtIndex:x]];
NSArray *filtered  = [objectArray filteredArrayUsingPredicate:predicate];

You should use %K to for unit.realTime and not %@.

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

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.