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

标签 ios swift realm

我正在使用 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/

相关文章:

ios - 如何从输入日期开始计算准确的完成天数

ios - 使用 Swift 根据 future 日期运行任务,包括应用程序何时处于后台

iOS、 swift 、XCode : Why is the position of my elements different for different devices?

swift - 观察 Realm 变化不触发

ios - 在后台或通知进程中使用加密 Realm ?

swift - Realm 列名与 Swift 变量名

ios - 在序列之间存储数据

ios - 选项卡栏、 View Controller 和 Storyboard

ios - 环球银行金融电信协会3 : How to add headers to columns in UITableViewController?

ios - 在 UIView 中绘制圆形透明 View