开发者

Create NSPredicate that passes an object to an instance method

I have created an NSManagedObject called TItem. To that object, I've added a helper instance method like the following:

- (BOOL) isItemForUser:(TUser *)user;

isItemForUser compares various properties in the TUser object to itself and returns a BOOL based on the results of the comparison.

I want to create a predicate that will be able to pass a TUser object to that method so that I can find all TItem objects that would return YES with a given object passed. In essence, I want to do something like the following:

fetchRequest.predicate = [NSPre开发者_如何学Pythondicate predicateWithFormat:@"isItemForUser:%@ == YES",self.user];

The above syntax is obviously not correct, but I am looking for a something in the same spirit. Any ideas?

Thanks!


I would explore doing whatever comparisons done in your isItemForUser method in the predicate instead. Perhaps simplifying your data model to make that comparison easier? For instance, adding some new attribute that maps to a user and is calculated when the object is created. That way, the comparison is very simple.


FUNCTION is what you're looking for:

[NSPredicate predicateWithFormat:@"FUNCTION(SELF, 'isItemForUser:', %@) == YES", self.user]

Two things to note, however:

  1. isItemForUser: will have to return an NSNumber, not a BOOL
  2. I'm 99.9999% sure this will not work as part of a fetch request's predicate
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜