ios - 从我的 CoreData 获取数据并使用 Swift 将其呈现在 TableView 中时出现 fatal error

标签 ios swift uitableview core-data fatal-error

我一直在尝试找到一种方法从 CoreData 中获取数据并将其呈现在 TableView 的单元格中。单元格有标题和副标题,其中标题包含内容的主题,副标题包含日期。我尝试了几种不同的方法,我采用的路径没有给我任何语法错误,但我收到了调试错误。

public class SecondViewController: UIViewController, UITableViewDelegate, UISearchBarDelegate, UISearchDisplayDelegate, UITableViewDataSource, NSFetchedResultsControllerDelegate {

    public var context: NSManagedObjectContext!

    @IBOutlet weak var tableView: UITableView!

    lazy var fetchedResultsController: NSFetchedResultsController = {
        let dataFetchedRequest = NSFetchRequest(entityName: "Data")
        let primarySortDescriptor = NSSortDescriptor(key: "Data.subject", ascending: true)
        dataFetchedRequest.sortDescriptors = [primarySortDescriptor]

        let frc = NSFetchedResultsController(
            fetchRequest: dataFetchedRequest,
            managedObjectContext: self.context,
            sectionNameKeyPath: "Data.subject",
            cacheName: nil)

        frc.delegate = self

        return frc

        }()

    override public func viewDidLoad() {
        var error: NSError? = nil
        if (fetchedResultsController.performFetch(&error) == false) {
            print("An error occurred: \(error?.localizedDescription)")
        }
    }

    public func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        if let sections = fetchedResultsController.sections {
            return sections.count
        }

        return 0
    }
    public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let sections = fetchedResultsController.sections {
            let currentSection = sections[section] as! NSFetchedResultsSectionInfo
            return currentSection.numberOfObjects
        }

        return 0
    }

    public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
        let data = fetchedResultsController.objectAtIndexPath(indexPath) as! Data

        cell.textLabel?.text = data.subject
        cell.detailTextLabel?.text = data.date

        return cell
    }
}

let frc = NSFetchedResultsController(
        fetchRequest: dataFetchedRequest,
        **managedObjectContext: self.context,** <-- This line gives me error
        sectionNameKeyPath: "Data.subject",
        cacheName: nil)

error: fatal error: unexpectedly found nil while unwrapping an Optional value

最佳答案

您拥有特性:

public var context: NSManagedObjectContext!

您永远不会将其设置为 NSManagedObjectContext,因此它为零。这就是你致命且意外地发现的 nil。

关于ios - 从我的 CoreData 获取数据并使用 Swift 将其呈现在 TableView 中时出现 fatal error ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31364172/

相关文章:

android - FlashBuilder Away3D iOS -

ios - 如何使用collectionview流布局来获得正确的 View ?

ios - 找不到 Pods.modulemap - 在错误的目录中查找

ios - 使用swift 4将字典传递到 TableView 中

iphone - 在可编辑的 uitable 中移动某些行后出错

ios - 没有网络连接时 React Native IOS App 崩溃

ios - 使用 LocationManager 时减少电池使用

ios - 如何等待第一次调度完成执行

swift - 为什么我在 Swift 中不断收到此错误? 'Attemped to add a SKNode which already has a parent'

ios - 核心数据: How can I update a TableView field from an alert?