2012-02-26 67 views
0

我搜索了整個互聯網,但找不到任何有關如何在NSPredicate中使用NSExpression的好解釋。我知道我的謂詞需要內置函數abs。我有這個謂詞:如何使用NSExpression與NSPredicate

NSPredicate *predicate=[NSPredicate predicateWithFormat:@"hg-%f<0.000001",[txtInput.text floatValue]]; 

我需要hg- [txtInput.text floatValue]部分的絕對值。我如何實現它?

回答

2

添加abs()周圍的表達你希望你的謂語字符串

NSPredicate *predicate=[NSPredicate predicateWithFormat:@"abs(hg-%f)<0.000001",[txtInput.text floatValue]]; 
+0

感謝大衛體內部的絕對價值。當我看到它工作時,我感到非常驚訝,因爲abs似乎不是NSPredicate的功能之一。它相當於NSExpression中的一個函數。從這我推斷我們可以在NSPredicate字符串格式部分使用任何NSExpression表達式。 – 2012-02-27 06:25:24

+0

如果你看看Cocoa謂詞的BNF定義(https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Predicates/Articles/pBNF.html),你會看到你可以使用一組謂詞字符串中的函數(sum,count,min,max,average,median,mode,stddev,sort,log,ln,exp,floor,ceiling,abs,trunc,random,randomn,now) – 2012-02-27 07:09:42