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`.'
The error message is indicating that T
has been inferred as Object
rather than MyModel
, so you will need to adjust the call site to ensure Swift picks the correct type.
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With