gpt4 book ai didi

ios - fatal error : unexpectedly found nil while unwrapping an Optional value when assigning a value from an array to a UILabel

转载 作者:搜寻专家 更新时间:2023-11-01 07:29:22 26 4
gpt4 key购买 nike

这是我第一个 ViewController 中的代码

FirstViewController.

@IBOutletweakvar textField: UITextField!

array = [String]()

...

array.append(textField.text!)

...

overridefunc prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

let secondScene = segue.destinationViewController as! SecondViewController

secondScene.inputArray = inputArray

这是我的第二个 ViewController 中的代码。

SecondViewController

@IBOutletweakvar label: UILabel!

array = [String]()

...

print(array) // getting the value that was input by the user

someLabel.text = array[0] // on this line I am getting there error about getting nil

因此,我在第一个场景中从 textFields 向数组输入值,我试图将用户在第一个场景中输入的所有内容打印到第二个场景中的标签。当我尝试从数组中提取值并将其分配给 Label.text 时,出现错误: fatal error :在展开可选值时意外发现 nil

我真的不知道为什么会这样。

最佳答案

如果我正确理解你的缩写代码,你将在 FirstViewController 中填充 array,但传递 inputArray

并且在 SecondViewController 中,您不读取 inputArray,而是读取 array

您需要填充、传递和读取同一个数组。

关于ios - fatal error : unexpectedly found nil while unwrapping an Optional value when assigning a value from an array to a UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33662615/

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