[英]Swift 3 - Object type 'RealmSwiftObject' is not managed by the Realm exception
I am using Realm with Swift 3 in my iOS app. 我在我的iOS应用程序中使用Realm和Swift 3。 I have the following code 我有以下代码
//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 }
}
where T
in this context is my realm model class which look like 其中T
在这个上下文中是我的领域模型类,看起来像
class MyModel : Object {
dynamic var id = 0
dynamic var date = NSDate()
override class func primaryKey() -> String? {
return "id"
}
}
I am getting an exception at run time saying 我在运行时说得到例外
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选择正确的类型。
Hi just resolved such issue. 嗨刚解决了这个问题。 In my case if I call your func "findForToday" like 在我的情况下,如果我称你的功能“findForToday”
let myArray = MyModel().findForToday() and i got same error 让myArray = MyModel()。findForToday(),我得到了同样的错误
but after i specified value type error lost. 但在我指定值类型错误丢失后。
let myArray : AnyRealmCollection < MyModel> = MyModel().findForToday() let myArray:AnyRealmCollection <MyModel> = MyModel()。findForToday()
The question is pretty old but it looks like you did not add your object to the Realm configuration: 问题很旧但看起来你没有将你的对象添加到Realm配置:
var realmOnDisc: Realm? {
let url = ...URL to your file
let objectTypes = [MyModel.self, SomeOtherModel.self]
let config = Realm.Configuration(fileURL: url,
deleteRealmIfMigrationNeeded:true,
objectTypes: objectTypes)
do {
let realm = try Realm(configuration: config)
return realm
} catch let error {
log.error("\(error)")
return nil
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.