条件绑定的初始化程序必须具有可选类型,而不是“()”
问题描述:
我无法解决代码中的“如果发生”问题。根据错误说它必须使用可选类型?条件绑定的初始化程序必须具有可选类型,而不是“()”
@IBAction func operate(sender: UIButton) {
if userIsInTheMiddleOfTypingANumber{
enter()
}
if let operation = sender.currentTitle {
if let result = calcModel.performOperation(operation){
displayValue = result
}else {
displayValue = 0
}
}
}
答
我怀疑你的问题是在这条线:
if let result = calcModel.performOperation(operation)
你performOperation
函数的返回类型为()
,或者无效,这意味着它不返回任何东西。机会是,得到错误的称号,在calcModel
您performOperation
功能具有以下特征:
func performOperation(operation: String) {
注意缺少一个箭头和类型。如果你要返回结果,签名应该是这样的:
func performOperation(operation: String) -> Int {
你只需要if let
,如果你决定回到Int?
。
谢谢乔希!我想我发现了错误。我忘记使performOperation返回值。 –
不客气,欢迎来到Stack Overflow。当答案解决您的问题时,您可以使用其旁边的灰色勾号按钮将其标记为接受的答案,并且有些人也选择投票。 –