2017-02-14 41 views
0

我對初學者進行了一個快速的開發過程,並試圖創建一個非常簡單的應用程序,該應用程序一旦按下按鈕即可用輸入文本創建新任務,但遇到一些錯誤我似乎無法理解。從聲明的變量引用核心數據屬性

錯誤發生在我的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") 
    } 
} 
} 
+0

請研究如何在覈心數據中保存和提取值,那裏有很多精彩的教程。 –

+0

核心數據應該有[NSManagedObjects]類型的數組。 –

+0

好的,我會嘗試查找更多的信息。我仍然非常好奇,因爲教練也使用Swift 3和XCode 9,並且適用於他。 –

回答

1

任務是Taskentities的數組,你可能意味着訪問task.corename不tasks.corename

if (task.isImportant == true){ 
    cell.textLabel?.text = " \(task.corename!)" 

} else { 
    cell.textLabel?.text = task.corename! 
} 

而對於TableViewDelegate問題,只要確保實現所有必要的funcs ...你缺少1:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 0 
} 
+0

好的觀察,但我仍然沒有得到它是這東西新的迅速3?我看不到他的核心數據保存功能 –

+0

他可能從不同的課程中保存它。但是這肯定是爲什麼他得到他有編譯時錯誤(截圖)@TusharSharma – toiavalle

+1

可能是可能的。我想問題是這樣做的,應該標記爲綠色,如果工作很快。 –