gpt4 book ai didi

swift - 在 Realm 中存储自定义对象数组

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

我有一个名为 List 的对象它是 Realm 的 Object 的子类类:

class List: Object {
dynamic var brandListItems: [BrandListItem] = []
}

和另一个对象,BrandListItem这也是 Object 的子类:

class BrandListItem: Object {
dynamic var brandID: String?
dynamic var name: String?
}

我的应用因以下错误而崩溃

'Property 'brandListItems' is declared as 'NSArray', which is not a supported RLMObject property type. All properties must be primitives, NSString, NSDate, NSData, NSNumber, RLMArray, RLMLinkingObjects, or subclasses of RLMObject.

我尝试做类似 RLMArray<BrandListItem>() 的事情没有运气。如何将这些对象成功保存到 Realm?

最佳答案

你需要使用 Realm 的 List<T>属性(property)。请注意,它没有标记为 dynamic

https://realm.io/docs/swift/latest/#to-many-relationships

class List: Object {
let brandListItems = RealmSwift.List<BrandListItem>()
}

请注意,有必要限定 Realm Swift 的 List<T>用它的模块来消除它与你新声明的 List 的歧义类。

关于swift - 在 Realm 中存储自定义对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42960421/

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