我對初學者進行了一個快速的開發過程,並試圖創建一個非常簡單的應用程序,該應用程序一旦按下按鈕即可用輸入文本創建新任務,但遇到一些錯誤我似乎無法理解。從聲明的變量引用核心數據屬性
錯誤發生在我的ViewController和編輯器告訴我,我的核心數據實體並不擁有一個名爲「corename」的屬性,雖然它很好。
這裏是錯誤的截圖:3 errors
這裏是我的代碼:
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var tasks : [Taskentity] = []
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
// Do any additional setup after loading the view, typically from a nib.
}
override func viewWillAppear(_ animated: Bool) {
//Get the data from Core data
getData()
//Reload the table view
tableView.reloadData()
}
func numberOfSections(in tableView: UITableView) -> Int {
return tasks.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath : IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let task = tasks[indexPath.row]
if (task.isImportant == true){
cell.textLabel?.text = " \(tasks.corename!)"
} else {
cell.textLabel?.text = tasks.corename!
}
return cell
}
func getData() {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
do {
tasks = try context.fetch(Taskentity.fetchRequest())
} catch {
print("Fetching Data")
}
}
}
請研究如何在覈心數據中保存和提取值,那裏有很多精彩的教程。 –
核心數據應該有[NSManagedObjects]類型的數組。 –
好的,我會嘗試查找更多的信息。我仍然非常好奇,因爲教練也使用Swift 3和XCode 9,並且適用於他。 –