swift3核心数据获取崩溃

时间:2017-11-23 13:04:46

标签: ios iphone swift core-data

我正在将数据插入表中,其插入成功,

但是当我获取所有数据然后获取成功的实体数组

但是当我使用实体时,我检查实体的nil。属性是字符串类型,当我做" Entity.attributeName"当我使用选项var来存储属性值时,app崩溃了。

  

错误是" libc ++ abi.dylib:以未捕获的类型异常终止   NSException"

我不明白为什么我在检查时已经检查实体对象

请建议我如何在swift3中调试和处理这种类型的异常。

 /// fetch method to Fetch data 

func fetchRecordsForEntity(_ entity: String, inManagedObjectContext managedObjectContext: NSManagedObjectContext) -> [NSManagedObject] {
        // Create Fetch Request
        let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: entity)
        fetchRequest.returnsObjectsAsFaults = false
        // Helpers
        var result = [NSManagedObject]()

        do {
            // Execute Fetch Request
            let records = try managedObjectContext.fetch(fetchRequest)

            if let records = records as? [CityEntity] {
                result = records
            }

        } catch {
            print("Unable to fetch managed objects for entity \(entity).")
        }

        return result
    }


/// where app crash
  let cityEntity:CityEntity? = self.allCity![indexPath.row]
             let cityName:String? = cityEntity?.cn_city_name

0 个答案:

没有答案