gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 13:09:36 32 4
gpt4 key购买 nike

我是 IOS 编程和整个编程的初学者。

(我有一个 XCODE 6.4)

我的 TableViewController 中有一个代码,其中有一个数据,我想将其传递给另一个 viewController。我在互联网上阅读了很多关于它的内容,而不是我编写了这段代码:

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

let identifier = "formuleTableViewCell"

let cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath) as! formule



let formuleCommand = formulesList[indexPath.row]

// Configure the cell...

var shortCut = formuleCommand.formuleText

cell.formuleLabel.text = shortCut



return cell
}






var valueToPass:String!

func tablView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
println("You selected cell #\(indexPath.row)!")

// Get Cell Label
let indexPath = tableView.indexPathForSelectedRow();
let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell!;

let identifier = "formuleTableViewCell"

let cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath!) as! formule

valueToPass = cell.formuleLabel.text
performSegueWithIdentifier("detail", sender: self)

}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

if (segue.identifier == "detail") {

// initialize new view controller and cast it as your view controller
var viewController = segue.destinationViewController as! specialitiesViewController
// your new view controller should have property that will store passed value
viewController.passedValue = valueToPass
}

}

在我的 TableViewController 中,我有单元格,其中包含带有一些数据的标签。我想将数据从这个标签传递到另一个 ViewController。当我运行我的应用程序时,没有错误,但是当我单击其中一个单元格时,它必须将我移动到另一个 ViewController(我为此设置了一个 segue)打印错误: fatal error :意外在展开可选值时发现 nil和应用程序崩溃。请帮我解决这个错误!

感谢您的建议!

最佳答案

您已将 valueToPass 定义为隐式展开的可选值 - 即,您告诉 iOS 此处始终会有一个值。

因此,当 prepareForSegue 被调用时,如果您没有将 valueToPass 设置为任何值,它将失败。你应该用一些值初始化它。

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

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