[英]Realm: Query with variable in Swift 2
absolute newbie-question: I have a model that consists of two objects in a Realm-database: Person and Dog. 绝对的新手问题:我有一个模型,该模型包含一个Realm数据库中的两个对象:Person和Dog。 Person contains a list of Dogs. 人包含狗的列表。 How can I query/filter for a list of dogs when the name of the Person is being handed over from a previous VC? 从先前的VC移交Person的名称时,如何查询/过滤狗列表? For a tableView I need the query at the top of my VC-code. 对于tableView,我需要在VC代码顶部进行查询。 To access a single Person, I tried 要访问一个人,我尝试了
personName: String! (derived from previous VC)
let predicate = NSPredicate(format: "name = %@", personName)
let currentPerson = try! Realm().objects(Person).filter(predicate)
but I get 但我明白了
Instance member 'personName' cannot be used on type 'dogsVC' 实例成员'personName'不能用于'dogsVC'类型
What I want is simply list all the dogs of a specific person in a tableView. 我想要的只是在tableView中列出特定人的所有狗。
Thanks in advance! 提前致谢!
You will need to instantiate your predicate in viewDidLoad
for example. 例如,您将需要在viewDidLoad
实例化谓词。 Or make it a computed property: 或使其成为计算属性:
var predicate:NSPredicate {
return NSPredicate(format: "name = %@", personName)
}
And that's because you need to wait for your class to initialize before using self
. 那是因为在使用self
之前,您需要等待类初始化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.