gpt4 book ai didi

Swift:将四个 Double 变量相乘时出错

转载 作者:行者123 更新时间:2023-11-28 05:33:48 25 4
gpt4 key购买 nike

我在“AnalyzeInput”类的返回语句中不断收到错误消息,“无法使用类型为‘($T18, Double)’的参数列表调用‘*’”,这毫无意义,因为所有四个变量在其方法头中被定义为 Double。这是两个相关的类,删除了不相关的代码:

class AnalyzeInput {

// Constructor, initialize InputInfo, add information to it etc etc

var remainingToAnalyze: String
var information: InputInfo

func findOutput() {
return information.getVolumeAmount() * information.getMeasurement().getMiliplier() * information.getConcentration() * information.getCompoundMolarMass()
}
}

class InputInfo {
var volumeAmount: Double = 0
var measurementType: Measurement = Measurement(short: "null", singular: "null", plural: "null", multiplier: 0)
var concentration: Double = 0
var compoundMolarMass: Double = 0

init () {
}

func setVolumeAmount(volumeAmount: Double) {
self.volumeAmount = volumeAmount
}

func setMeasurementType(measurement: Measurement) {
self.measurementType = measurement
}

func setConcentration(concentration: Double) {
self.concentration = concentration
}

func setCompound(compoundMolarMass: Double) {
self.compoundMolarMass = compoundMolarMass
}

func getVolumeAmount() -> Double {
return volumeAmount
}

func getMeasurement() -> Measurement {
return measurementType
}

func getConcentration() -> Double {
return concentration
}

func getCompoundMolarMass() -> Double {
return compoundMolarMass
}
}

如何修复错误?

最佳答案

findOutput 的返回类型不是 Double。如果你想从它返回一个 double,你需要正确声明返回类型。此外,您不需要第二类中的所有这些 getter/setter 方法——您可以只使用点运算符来访问属性。最后,“multiplier”在您的回电中拼写错误。所以:

func findOutput() -> Double {
return information.volumeAmount * information.measurement.multiplier * information.concentration * information.compoundMolarMass
}

关于Swift:将四个 Double 变量相乘时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26091869/

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