ios - 如何在创建 Realm 对象之前验证值(在 RealmSwift 中)

标签 ios swift realm

我的应用程序代码从服务器获取 JSON 数据,将其转换为字典,然后使用它来组合并尝试保存具有匹配模式的 RealmSwift 对象。

我遇到了由于 Float 值进入模型中声明为 Int 的字段而导致的崩溃。 RLMException 被抛出。

this thread 中的建议, 人们不会简单地 try catch RLMException

我的问题是,这个答案正确吗?而且,如果是这样,当发现意外值时防止崩溃的正确方法是什么?难道没有某种验证机制可以让我在尝试设置所有值之前运行它们吗?

最佳答案

您可以使用第三方框架将 JSON 映射到 Realm 对象,例如 ObjectMapper并使用它的可失败初始化程序来验证您的 JSON。

This failable initializer can be used for JSON validation prior to object serialization. Returning nil within the function will prevent the mapping from occuring. You can inspect the JSON stored within the Map object to do your validation.

关于ios - 如何在创建 Realm 对象之前验证值(在 RealmSwift 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37851859/

相关文章:

ios - CoreData 上的 Realm 我应该使用 NSFetchedResultController 还是字典?

iOS:以 png 或 jpeg 格式写入 CGImageRef 磁盘

javascript - Titanium 音乐播放器进度事件监听器状态未触发

iOS tableview 单元格按钮选择奇怪的行为

ios - Realm, Swift, 多对多关系

android - 从 Realm.io android 中删除单个对象(如果它仍然存在)

ios - 是否可以为对象中的字典值创建 NSSortDescriptor?

ios - 通过 Appdelegate 调用时展开 View Controller 时出错

swift 包 --verbose generate-xcodeproj 给出错误 : reachedTimeLimit

swift - 分配 backgroundColor 时,我的带有 UIVibrancyEffect 的 UIButton 的标题不可见