I am using Realm with Swift 3 in my iOS app. 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
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
let myArray = MyModel().findForToday() and i got same error
but after i specified value type error lost.
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:
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
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.