基于计数列表<t>筛选领域对象

我试图根据匹配对象的数量来查询一个类。 但是,我不断收到以下错误'Unable to parse the format string "matches.@count > 0"' 。 我试过以下代码从文档,但它没有帮助。 我想要的是获得所有联赛,其中有超过0比赛?

 class League:Object { dynamic var id: Int = 0 dynamic var name: String? = "" var matches = List<Match>() override class func primaryKey() -> String { return "id" } } class Match:Object { dynamic var matchId: Int = 0 dynamic var date: NSDate = NSDate() dynamic var homeName: String? = "" dynamic var awayName: String? = "" dynamic var homeAcro: String? = "" dynamic var awayAcro: String? = "" dynamic var awayScore: Int = 0 dynamic var homeScore: Int = 0 dynamic var leagueName: String? = "" dynamic var homeLogo: NSData = NSData() dynamic var awayLogo: NSData = NSData() override class func primaryKey() -> String { return "matchId" } } 

试过的代码

 let realm = try! Realm() let predicate = NSPredicate(format: "matches.@count > 0") menuArray = realm.objects(League).sorted("id").filter(predicate) 

MATCHESNSPredicate语法中的保留字 。 你可以用#作为保留字的前缀来转义它们,如下所示:

 let realm = try! Realm() let predicate = NSPredicate(format: "#matches.@count > 0") menuArray = realm.objects(League).sorted("id").filter(predicate)