Own function (round for NSString) in NSPredicate

大憨熊 提交于 2019-12-25 02:20:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!