所以我对领域来说还很陌生,我觉得我的问题很基础,但是我找不到答案。
基本上,我正在尝试查询与特定playerName
关联的所有TeamID
的Realm(即TeamID
不是主键),但是我很难找到解决方案。我不断收到Value of type 'Results<playerInfoTable>' has no member 'playerName'
错误。
下面是我的境界类:
`class playerInfoTable:对象{
@objc dynamic var playerID: Int = 0
@objc dynamic var playerName: String = ""
@objc dynamic var jerseyNum: Int = 0
@objc dynamic var TeamID: String = ""
@objc dynamic var goalCount: Int = 0
@objc dynamic var shotCount: Int = 0
override class func primaryKey() -> String {
return "playerID"
}
}`
和代码我使用的查询领域:
let mainPlayerFilter = NSPredicate(format: "teamID == %@", "1")
let mainPlayerStrings = realm.objects(playerInfoTable.self).filter(mainPlayerFilter)
let mainPlayerTeamName = mainPlayerStrings.playerName
解决方案!
let mainPlayerFilter = NSPredicate(format: "TeamID == %@", String(homeTeam!))
let mainPlayerStrings = realm.objects(playerInfoTable.self).filter(mainPlayerFilter)
let mainPlayerTeamName = mainPlayerStrings.value(forKeyPath: "playerName") as! [String]
答案 0 :(得分:0)
我想,当你声明mainPlayerTeamName常数你的错误。请尝试以下操作:
let mainPlayerTeamName = mainPlayerStrings.first?.playerName
我注意到在您的playerInfoTable类中,您将“ teamID”声明为“ TeamID”,而在您的谓词中,您将其称为“ teamID”。确定您要哪个,因为NSPredicate默认情况下区分大小写。