我保存的對象中的核心數據,我期待如何將這些對象作爲一個字典如何在Swift中將核心數據對象讀入Dictionary?
這裏取的是我的代碼示例,其中部分是用於字典和公司作爲核心數據對象的數組鍵。
private var companies = Dictionary<String, Array<Company>>()
private var sections: [String] = ["Pending", "Active", "Pending"]
override func viewWillAppear(_ animated: Bool) {
let fetchRequest : NSFetchRequest<Company> = Company.fetchRequest()
let moc = DatabaseController.getContext()
do {
let request = try moc.fetch(fetchRequest)
for case let (index, object) in request.enumerated() {
companies[sections[index]]!.append(object)
}
} catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
}}
當我試圖執行我的代碼,我有一個錯誤:
fatal error: unexpectedly found nil while unwrapping an Optional value
誰能幫我這個問題?
根據提取請求,提取的對象始終以**數組**的形式返回,可以是[NSManagedObject]或[[String:Any]]。 – vadian