问题
I got problem with NSPredicate.
I got predicate:
NSPredicate* pr = [NSPredicate predicateWithFormat:@"FUNCTION('12.12345','round',3) = 12.123"];
and defined extension for NSString:
-(double) round:(NSNumber*) precision;
When I call manually:
NSLog(@"%f",[s round:3])
where s is NSString, it's working prefectly, but used in NSPredicate rise: NSInvalidArgumentException: rease -[NSCFString round]: unrecognized selector sent to instance.
I use solution used on page: http://funwithobjc.tumblr.com/post/2922267976/using-custom-functions-with-nsexpression
The main question is to make round function in NSPredicate, so any other solutions are welcome
回答1:
The colon (:
) must be part of the selector in the FUNCTION
, so it should be FUNCTION('12.12345', 'round:', 3)
Also, I believe the round:
method should return an NSNumber *
, not a primitive type.
来源:https://stackoverflow.com/questions/7556882/own-function-round-for-nsstring-in-nspredicate