2013-04-03 30 views
1

我一直在環顧四周,看到類似於我正在做的但不能讓他們工作的例子。我有一個「產品」核心數據實體,它與「製造商」實體具有多對多關係。 「製造商」有一個我想要搜索的屬性「名稱」。 「產品」還具有我希望搜索的「isCustomItem」屬性。那麼,我就是我想達到的樣子:NSPredicate searching NSSet

Product 1 ... M Manufacturer.nameProduct.isCustomItem == 0

這是我已經成功地扎堆至今:

NSPredicate *p3 = [NSPredicate predicateWithFormat:@"SUBQUERY(manufacturer,$m,$m.name CONTAINS[c] %@) AND (isCustomItem == 0)", searchString]; 

不過,我不斷收到錯誤:

**'Unable to parse the format string "SUBQUERY(manufacturer,$m,$m.name CONTAINS[c] %@) AND (isCustomItem == 0)"'** 

回答

4

嘗試這樣的事情...

NSPredicate *namePredicate = [NSPredicate predicateWithFormat:@"ANY manufacturer.name CONTAINS[c] %@", searchString]; 
NSPredicate *customPredicate = [NSPredicate predicateWithFormat:@"isCustomItem == 0"]; 

NSPredicate *compoundPredicate = [NSCompoundPredicate andPredicateWithSubPredicates:@[namePredicate, customPredicate]]; 

然後使用compoundPredicate來過濾您的設置。

+0

感謝您的快速回復。這樣做時我仍然得到這個錯誤:'無法解析格式字符串'任何manufacturer.name CONTAINS [c] @%「' – random

+1

它應該是'%@',而不是'@%'。 –

+0

Woops,現在固定。謝謝 – Fogmeister