从声明的变量

时间:2017-02-14 03:39:04

标签: ios swift core-data swift3

我正在为初学者开设一个快速的开发课程,并且我试图创建一个非常简单的应用程序,一旦按下按钮就会创建带有输入文本的新任务,但我遇到了一些错误,我可以&#39似乎很明白。

错误发生在我的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")
    }
}
}

1 个答案:

答案 0 :(得分:1)

任务是一个Taskentities数组,您可能打算访问task.corename而不是tasks.corename

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

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

对于TableViewDelegate问题,只需确保实现所有必要的功能......你缺少1:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 0
}
相关问题