gpt4 book ai didi

swift - 更新 xcode fatal error : unexpectedly found nil while unwrapping an Optional value 后

转载 作者:搜寻专家 更新时间:2023-11-01 05:39:58 25 4
gpt4 key购买 nike

更新后程序停止工作。

import UIKit
import CoreData

class ViewController: UIViewController, UITableViewDataSource, UINavigationControllerDelegate, UITableViewDelegate {

var interstring = [LogItem]()

let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext

var logTableView = UITableView()

let photoVC = PhotoViewController()

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
super.viewDidLoad()
title = "\"LogItem\""
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
}

func tableView(tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
return interstring.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

***let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! UITableViewCell*** - This emphasizes this part of the code

let logItem = interstring[indexPath.row]
cell.textLabel!.text = logItem.valueForKey("title") as? String

return cell
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)

let appDelegate =
UIApplication.sharedApplication().delegate as! AppDelegate

let managedContext = appDelegate.managedObjectContext!

let fetchRequest = NSFetchRequest(entityName:"LogItem")

var error: NSError?

let fetchedResults =
managedContext.executeFetchRequest(fetchRequest,
error: &error) as? [LogItem]
println("\(interstring)")

if let results = fetchedResults {
interstring = results
//self.tableView.reloadData()
} else {
println("Could not fetch \(error), \(error!.userInfo)")
}
}

所有项目都是 here

最佳答案

我在更新到 swift 2 和 xcode 7 时也遇到了同样的问题。

我认为问题在于您正在尝试调用一个不存在的单元格。我通过使用自定义单元格创建 Nib 然后将标识符设置为“单元格”以及初始化 Nib 来解决问题

let identifier = "cell"
var cell: customCell! = tableView.dequeueReusableCellWithIdentifier(identifier) as? customCell

if cell == nil {

tableView.registerNib(UINib(nibName: "customCell", bundle: nil), forCellReuseIdentifier: identifier)
cell = tableView.dequeueReusableCellWithIdentifier(identifier) as? customCell

}

希望对你有帮助

关于swift - 更新 xcode fatal error : unexpectedly found nil while unwrapping an Optional value 后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31871554/

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