gpt4 book ai didi

ios - 使用结构体时不断收到 "Cannot assign to value: 'calculateBMI' is a method"

转载 作者:行者123 更新时间:2023-12-03 21:25:00 27 4
gpt4 key购买 nike

使用结构属性时,我不断收到“无法分配给值:‘calculateBMI’是一种方法”错误消息。任何解决这个问题的方法。这是我的结构代码:

import UIKit

struct Calculations {

var bmi : Float = 0.0

func getBMIValue() -> String {
let BMIRounded = String(format: "%.1f", bmi)
return BMIRounded
}

mutating func calculateBMI (height: Float, weight: Float) {
bmi = weight / (height * height)
}
}

这是我在第一个页面 View Controller 上收到错误消息的地方:

import UIKit

class HomeViewController: UIViewController {

var calculations = Calculations()

@IBOutlet weak var heightLabel: UILabel!
@IBOutlet weak var weightLabel: UILabel!
@IBOutlet weak var heightSlider: UISlider!
@IBOutlet weak var weightSlider: UISlider!


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

@IBAction func HeightSliderChange(_ sender: UISlider) {
var height = String (format: "%.2f", sender.value)
heightLabel.text = "\(height)m"
print(height)

}

@IBAction func WeightSliderChange(_ sender: UISlider) {
var weight = String (format: "%.0f", sender.value)
weightLabel.text = "\(weight)kg"

}

@IBAction func calculatePressed(_ sender: Any) {
let height = heightSlider.value
let weight = weightSlider.value

calculations.calculateBMI = (height: height, weight: weight)
self.performSegue(withIdentifier: "GettingResults", sender: self)


}
}

错误发生在第 43 行(calculations.calculateBMI)

最佳答案

由于错误清楚地表明您正在尝试设置calculateBMI,它是一个函数而不是变量。要解决此问题,请修改您的 calculatePressed 方法,如下所示:

@IBAction func calculatePressed(_ sender: Any) {
let height = heightSlider.value
let weight = weightSlider.value

calculations.calculateBMI(height: height, weight: weight)
self.performSegue(withIdentifier: "GettingResults", sender: self)
}

关于ios - 使用结构体时不断收到 "Cannot assign to value: 'calculateBMI' is a method",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62119337/

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