gpt4 book ai didi

ios - 从声明的变量引用核心数据属性

转载 作者:行者123 更新时间:2023-11-28 15:51:42 26 4
gpt4 key购买 nike

我正在学习面向初学者的快速开发类(class),并尝试制作一个非常简单的应用程序,在按下按钮后使用输入的文本创建新任务,但我遇到了一些我似乎无法解决的错误明白了。

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

最佳答案

Tasks 是一个任务实体数组,您可能打算访问 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
}

关于ios - 从声明的变量引用核心数据属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42217466/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com