![](/img/trans.png)
[英]How to create predicate to fetch all entities with not repeated results for the same property?
[英]How to create predicate to filter entities on field with Transformable type?
我只是有實體Service
與物業students
。 該字段屬於.xcdatamodelm
中定義的可Transformable
類型。
在代碼中,我的屬性定義為[String]
:
@NSManaged var students: [String]
現在我需要過濾所有學生不為空的服務。
當我這樣做時:
let predicate = NSPredicate(format: "students.@count != 0")
let services = Service.mr_findAll(with: predicate) as! [Service]
但是這里出現錯誤:
*** 由於未捕獲的異常“NSInvalidArgumentException”而終止應用程序,原因:“不支持的 function 表達式計數:(學生)”
由於Transformable
字段可以保存任何數據,因此無法通過您的自定義類型使用謂詞進行獲取。 因此,您的所有請求都將使用瞬態屬性,但“您不能使用基於瞬態屬性的謂詞進行獲取(盡管您可以使用瞬態屬性自己在 memory 中進行過濾)。” 來自https://developer.apple.com/documentation/coredata/nspropertydescription 。
在 memory 中過濾數組的唯一方法:
let filtered = allServices.filter { $0.students.count > 0 }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.