ios – Swift 3 – 对象类型’RealmSwiftObject’不受Realm异常管理

前端之家收集整理的这篇文章主要介绍了ios – Swift 3 – 对象类型’RealmSwiftObject’不受Realm异常管理前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在我的iOS应用程序中使用 Realm和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在这个上下文中是我的领域模型类,看起来像

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相关文章