2013-02-02 17 views
2

我想要一個NSPredicate像這樣:減法NSPredicate

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(latitude - %@) < %@",coordsLatitude, kMaxLatLong]; 

我基本上要找到核心數據GPS COORDS來自userLocation一定距離內。

  • latitude是每個gps實體的緯度屬性。

  • coordsLatitude是userLocation的緯度。

  • kMaxLatLong設置爲1

我得到一個EXC_BAD_ACCESS這條線。我想這是因爲我的謂詞剛剛形成,特別是減法運算符。但是,我找不到任何說明如何使用NSPredicates進行減法的操作,以及NSExpressions。

回答

3

我會以相反的方式做到這一點。

計算出最小和最大緯度,然後使用謂詞找出它們之間的所有內容。

float coordsLatitude = //whatever 

float minLatitude = coordsLatitude - kMaxLatLong; 
float maxLatitude = coordsLatitude + kMaxLatLong; 

NSPredicate *minPredicate = [NSPredicate predicateWithFormat:@"latitude >= %f", minLatitude]; 
NSPredicate *maxPredicate = [NSPredicate predicateWithFormat:@"latitude <= %f", maxLatitude]; 

NSPredicate *compoundPredicate = [NSCompoundPredicate andPredicateWithSubPredicates:@[minPredicate, maxPredicate]]; 

然後在你的讀取請求使用compoundPredicate。

+0

好吧,我要試一試 - 非常有意義。但是,你知道是否有方法直接在謂詞中使用基本的數學運算符?只是好奇,因爲我看着,找不到任何東西,而且看起來很基本......我現在也意識到我需要在那裏粘貼絕對值函數。 – Ramsel

+0

我還沒有聽說過。儘管在任何你想使用基本的數學運算符的地方,你都可以在謂詞之外進行計算,就像我在這裏所做的那樣。 – Fogmeister

+0

下面是一些深入的NSPredicate過濾,它可以用於執行更復雜的查詢,比如屬性的abs():http://funwithobjc.tumblr.com/post/1677163679/creating-an-advanced-nspredicateeditorrowtemplate – TPoschel