我想知道爲什麼我無法將我的數據從核心數據添加到我的tableview。它是第一次添加和存儲,但當應用程序重新加載數據已經消失,所以我需要重新加載它,但我似乎無法將數據添加到我的tableview? 下面是代碼無法將類型'String'的值轉換爲期望的參數類型'NSManagedObject'Swift
var titlename: [NSManagedObject] = []
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let manageContext = appDelegate.persistentContainer.viewContext
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Items")
fetchRequest.returnsObjectsAsFaults = false
do {
let results = try manageContext.fetch(fetchRequest)
for result in results as! [NSManagedObject] {
if let theName = result.value(forKey: "name") as? String {
titlename.append(theName)
}
}
} catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
}
tableView.reloadData()
}