gpt4 book ai didi

ios - Swift 3 - 对象类型 'RealmSwiftObject' 不受 Realm 异常管理

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

我正在使用 Realm在我的 iOS 应用程序中使用 Swift 3。我有以下代码

//Find all records for the day
func findForToday<T: Object>() -> [T] {
let predicate = NSPredicate(format: "date >= %@ and date <= %@", DateUtil.dayStart(), DateUtil.dayEnd())
return getRealm().objects(T.self).filter(predicate).map { $0 }
}

这里的 T 是我的 Realm 模型类,它看起来像

class MyModel : Object {

dynamic var id = 0
dynamic var date = NSDate()

override class func primaryKey() -> String? {
return "id"
}

}

运行时出现异常

Terminating app due to uncaught exception 'RLMException', reason: 
'Object type 'RealmSwiftObject' is not managed by the Realm. If using a custom
`objectClasses` / `objectTypes` array in your configuration, add `RealmSwiftObject`
to the list of `objectClasses` / `objectTypes`.'

最佳答案

错误消息表明 T 已被推断为 Object 而不是 MyModel,因此您需要将调用站点调整为确保 Swift 选择正确的类型。

关于ios - Swift 3 - 对象类型 'RealmSwiftObject' 不受 Realm 异常管理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39960950/

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