开发者

NSpredicate with round function

开发者 https://www.devze.com 2023-04-07 22:24 出处:网络
I need to use NSPredicate with a round function. e.g. : Child* child1 = [self myFirstChild]; NSPredicate *predicate = [NSPredicate predicateWithString:@\"round(SELF.age) > 12\"];

I need to use NSPredicate with a round function. e.g. :

Child* child1 = [self myFirstChild];
NSPredicate *predicate = [NSPredicate predicateWithString:@"round(SELF.age) > 12"];
BOOL twelveOrAlmostTwelve = [predicate evaluateWithObject:chil开发者_开发技巧d1];

There is no such function in documentation: http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Predicates/Predicates.pdf

Any ideas? Thanks


You can use your own defined function in extensions class. IE:

[NSPredicate predicateWithFormat:@"FUNCTION('12.12345','round:',3) = 12.123"]

and you need to define this method in your category for ie. NSString:

-(NSNumber*) round:(NSString*) precision;

look at the page: http://funwithobjc.tumblr.com/post/2922267976/using-custom-functions-with-nsexpression


You cannot generate arbitrary predicate functions. In this case, however, the answer is trivial:

NSPredicate *predicate = [NSPredicate predicateWithString:@"SELF.age >= 11.5"];
0

精彩评论

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

关注公众号