gpt4 book ai didi

ios - 如何使用 realm swift 检索另一个对象列表中的对象?

转载 作者:搜寻专家 更新时间:2023-10-31 22:02:52 24 4
gpt4 key购买 nike

我有一些像这样的 Realm 类:

class Friends: Object {
dynamic var name = true
dynamic var role = true
dynamic var type = true
dynamic var owner: Profile?
}

class Profile: Object {
dynamic var uuid = NSUUID().UUIDString
dynamic var name = ""
dynamic var date = NSDate(timeIntervalSinceNow: 1)
dynamic var section = 0
dynamic var code = ""
dynamic var gender = 0
dynamic var type = ""
let friends = List<Friends>()

override static func primaryKey() -> String? {
return "uuid"
}
}

class Sub: Profile {
dynamic var owner: Master?
}

class Master: Object {
dynamic var type = ""
dynamic var name = ""
dynamic var date = ""
let subs = List<Sub>()
}

我知道要从 Realm 中检索对象,我必须这样做:

var master = try! Realm().objects(Master)
let profile = master[indexPath.row]
let date = profile.date
let name = profile.name
let type = profile.type

问题是:如何从“subs”(列表)中检索对象?

最佳答案

当您检索主对象时,您可以像访问任何其他属性一样访问其子列表:

let subs = profile.subs

这为您提供了一个可以迭代的列表:

for sub in profile.subs {
// do something with the sub object
}

或者您可以过滤子项以查找特定对象:

if let subjectWithId = profile.subs.filter("uuid == '7382a8d83'").first {
// do something with the subject
}

或者您可以使用下标通过索引访问元素:

let secondProfile = profile.subs[1]

关于ios - 如何使用 realm swift 检索另一个对象列表中的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33401347/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com