【问题标题】:Referencing core data attribute from declared variable从声明的变量中引用核心数据属性
【发布时间】:2017-02-14 03:39:04
【问题描述】:

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

错误发生在我的 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,并且对他有用。
  • 核心数据中的数据保存在哪里?你的核心数据模型类在哪里。
  • 他有Taskentity数组就可以了,只要Taskentity实现NSManagedObject @TusharSharma

标签: ios swift core-data swift3


【解决方案1】:

Tasks 是一个Taskentities 数组,您可能打算访问task.corename 而不是task.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
}

【讨论】:

  • 很好的观察,但我仍然不明白这是 swift 3 中的新东西吗??我看不到他的核心数据保存功能
  • 他可能是从另一个班级保存的。但这绝对是他得到编译时错误的原因(截图)@TusharSharma
  • 可能是可能的。好吧,我想问题已经解决了,如果可行的话,应该很快就会被标记为绿色。
  • 我是一个完整的初学者,我现在正在查看 Apple 关于核心数据的文档,我看到了我缺少的内容,但不知何故,它“应该”在没有 [NSManagedObjects 数组的情况下工作” ] 等等...我粘贴的代码缺少一个保存核心数据的函数。不过感谢您所做的一切。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-07
  • 2017-04-06
  • 1970-01-01
  • 2012-12-06
  • 1970-01-01
  • 2012-08-01
  • 2017-10-27
相关资源
最近更新 更多