gpt4 book ai didi

ios - 在 iOS 应用上显示计算错误时出错

转载 作者:行者123 更新时间:2023-12-01 19:37:08 25 4
gpt4 key购买 nike

我正在创建响应输入变量的 iOS 应用程序。

但是,打印结果时出现一条错误消息,我不知道要修复该错误。

第一条错误消息

        let result = x + y
label.text = "result is \(result) "

Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value



我试图做的

收到第一条评论后,我仔细检查了 ResultViewController.swift并且转换页面已更改。

我需要的是显示 result is 2而是 res...我该如何解决?

ViewController.swift

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// aquire ResultViewController from segue
let resultViewController:ResultViewController = segue.destination as! ResultViewController
// set numbers
resultViewController.x = 1
resultViewController.y = 1
}

@IBAction func unwind(_ segue: UIStoryboardSegue) {
// called after returning back from segue
}
}


ResultViewController.swift

import UIKit

class ResultViewController: UIViewController {

@IBOutlet weak var label: UILabel!

var x:Int = 0
var y:Int = 0

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.

let result = x + y
label.text = "result is \(result) "
}
}

最佳答案

从 Storyboard 或以编程方式在标签上添加约束

 override func viewDidLoad() {
super.viewDidLoad()

label.translatesAutoresizingMaskIntoConstraints = false
label.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
label.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
label.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
label.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true

let result = x + y
label.text = "result is \(result) "
}

或您想在标签上添加的任何约束

关于ios - 在 iOS 应用上显示计算错误时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59825897/

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