ios - CoreData - 安全检查 CoreData 对象的值

标签 ios swift core-data

按照我目前安排代码的方式,下一行将为各种托管对象上下文运行。一些获取的实体将具有“complededDate”,而其他实体将没有“completedDate”属性。

let task = retrieved_MgObjCntxt[(indexPath as NSIndexPath).row]
if let itemFinDate = task.value(forKey: "completedDate") {
      ...

我想通过使用 if-let,如果这失败了,那也没关系……但是我遇到了“lldb”崩溃。我一直在尝试使用 do/catch 和 throw 来解决它,但我对它们的工作原理不是很熟悉。

有没有一种方法可以安全地检查,以便如果“completedDate”不存在,它只是跳过 if-closure 中的代码?

最佳答案

let task = retrieved_MgObjCntxt[(indexPath as NSIndexPath).row]
if task.entity.propertiesByName.keys.contains("completeDate") {
    ...
}

关于ios - CoreData - 安全检查 CoreData 对象的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40324803/

相关文章:

ios - 添加新项目后 UITableView 未更新(使用核心数据)

html - Gmail iOS 应用程序使用自定义 html 标签 - 有任何信息吗?

ios - Swift 后台执行

ios - Swift iOS -UIImagePicker 的照片库在模拟器上显示,但在运行 Xcode 时在实际设备上崩溃(不会显示)

ios - Core Data Fetch Request 在模拟器上运行,但在设备上不运行

core-data - 核心数据: New added objects are not being added to the end of the array when getting the NSFetchRequest

ios - 无法安装 iPhone 应用程序

ios - 如何确定数组是否已包含一个值以及它是否跳到下一个值?

swift - SceneKit – 获取相机的方向

ios - 在 UIScrollView 中调整 View 大小