我试图根据匹配对象的数量来查询一个类。但是,我一直收到以下错误'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)
答案 0 :(得分:5)
NSPredicate
语法中的 MATCHES
is a reserved word。您可以使用#
为保留字添加前缀以逃避它们,如下所示:
let realm = try! Realm()
let predicate = NSPredicate(format: "#matches.@count > 0")
menuArray = realm.objects(League).sorted("id").filter(predicate)