根据代码打击,如何从Grapes
查询名为List 2
的项目?
我知道如何从List 2
获取所有项目,但我想要的是仅从Grapes
List 2
的项目
let itemsFromList2 = realm.objects(ShoppingList.self).filter("listName = 'List 2' ").first?.itemList
购物清单型号
import Foundation
import RealmSwift
class ShoppingList: Object{
dynamic var listName = ""
var itemList = List<Item>()
}
项目模型
import Foundation
import RealmSwift
class Item:Object {
dynamic var productName: String = ""
}
所有购物清单的输出( realm.objects(ShoppingList.self))
Lists: Results<ShoppingList> (
[0] ShoppingList {
listName = List 1;
itemList = RLMArray <0x6180000feb80> (
[0] Item {
productName = Tomatoes;
},
[1] Item {
productName = Grapes;
},
[2] Item {
productName = Oranges;
}
);
},
[1] ShoppingList {
listName = List 2;
itemList = RLMArray <0x6180000fec80> (
[0] Item {
productName = Tomatoes;
},
[1] Item {
productName = Grapes;
},
[2] Item {
productName = Oranges;
},
[3] Item {
productName = Green Peppers;
},
[4] Item {
productName = Apples;
}
);
}
)
答案 0 :(得分:1)
您可以将多个Realm过滤器链接在一起,从而直接过滤itemList
的{{1}}属性。领域过滤器也适用于List 2
,而不仅仅是List
。
Results
您使用let itemsFromList2 = realm.objects(ShoppingList.self).filter("listName = 'List 2' ").first?.itemList.filter("productName == 'Grapes'")
语法在NSPredicate
s中使用变量名称:
%@