swift - fatal error : unexpectedly found nil while unwrapping an Optional value: Swift, 核心数据

标签 swift core-data ios8.3

我在线上遇到错误: 让 indexPath = self.menuTable.indexPathForSelectedRow()!。 似乎我没有从 indexPathForSelectedRow 中获取值。我正在将 CSV 文件解析为 Core Data。不确定这是否重要。我是编码新手,所以不确定我是否遗漏了一些明显的东西。

import UIKit
import CoreData

class MenuTableViewController: UITableViewController {

    @IBOutlet var menuTable: UITableView!
    private var menuItems:[MenuItem] = []
    var fetchResultController:NSFetchedResultsController!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Load menu items from database
        if let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext {
            let fetchRequest = NSFetchRequest(entityName: "MenuItem")
            var e: NSError?
            menuItems = managedObjectContext.executeFetchRequest(fetchRequest, error: &e) as! [MenuItem]
            if e != nil {
                println("Failed to retrieve record: \(e!.localizedDescription)")
            }
        }

        // Make the cell self size
        self.tableView.estimatedRowHeight = 66.0
        self.tableView.rowHeight = UITableViewAutomaticDimension
        self.tableView.layoutIfNeeded()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // Return the number of sections.
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // Return the number of rows in the section.
        return menuItems.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = menuTable.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! MenuTableViewCell

        // Configure the cell...
        cell.nameLabel.text = menuItems[indexPath.row].name
        cell.detailLabel.text = menuItems[indexPath.row].detail
//        cell.priceLabel.text = "$\(menuItems[indexPath.row].price as! Double)"

        return cell
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
    {
        self.performSegueWithIdentifier("showFront", sender: self)

    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
    {
        if (segue.identifier == "showFront")
        {
            var upcoming: CardFrontViewController = segue.destinationViewController as! CardFrontViewController

            let indexPath = self.menuTable.indexPathForSelectedRow()!

            let titleString = menuItems[indexPath.row].name

            upcoming.titleStringViaSegue = titleString

            self.menuTable.deselectRowAtIndexPath(indexPath, animated: true)

        }
    }

}

最佳答案

由于您有 tableView:didSelectRowAtIndexPath: 的实现,并且单元格连接到 Storyboard 中的 segue,因此 segue 发生了两次。第二次执行 segue 时不会有任何选择,因为您在第一次 segue 期间取消选择它。您可以通过删除 tableView:didSelectRowAtIndexPath: 的实现来解决此问题,或者通过在 Storyboard中创建 segue 并将 View Controller 本身作为源而不是单元格并保留您对 segue 的手动调用。

关于swift - fatal error : unexpectedly found nil while unwrapping an Optional value: Swift, 核心数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33559316/

相关文章:

ios - NSCoreDataCoreSpotlightDelegate 索引现有数据

iphone - 将数据库直接加载到 CoreData

ios - 在 ios 8.3 及更高版本中,UIAlertView 导致 keyboardWillShow 和 keyboardWillHide 被调用两次

ios - 自定义 UITableViewCell 约束不起作用

ios - Swift 3 上传图片问题(格式)

swift - 如何将 Socket.IO-Client-Swift 导入 Swift 5 项目?

objective-c - 使用核心数据实体作为枚举?

swift - Firebase 问题 - 使用 iOS 8.3 的 iPod 上的实时数据库没有响应

ios - 使用类似 "tag"的单元格设置 collectionView

swift - 如何在 Swift 中将 UILabel 与多行文本居中?