gpt4 book ai didi

ios - 我收到诸如 "braced block of statements is an unused closure"和预期表达式之类的错误

转载 作者:行者123 更新时间:2023-11-29 01:28:33 27 4
gpt4 key购买 nike

这是我第一次在 swift 中做一个简单的项目,这些错误困扰了我几个小时。我在下面有这段代码,即使我在 if/else 中有花括号和语句,我仍然会遇到这些错误。任何帮助将不胜感激。

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var `switch`: UISwitch!
@IBOutlet var Answer: UILabel!
@IBOutlet var tempInput: UITextField!


//aqnswer value

@IBAction func switchPressed(sender: AnyObject)
{
if switch.on {

self.Answer.text = "cel to fah"
}
else {

self.Answer.text = "fah to cel"
}

}
//textfield value



@IBAction func calculate(sender: AnyObject)
{
//get user input
// value = celcius

var Value:Int = tempInput.text.toInt()!

var toFah :Int = ( 32 + Value * 9 ) / 5

//to celcius

var toCel: Int = (Value-32) * 5 / 9

if switch.on {



self.Answer.text = toFah.description
}

else {
self.Answer.text = toCel.description
}






// println(fah)

// Answer.text = fah.description


}



override func viewDidLoad() {

super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


}

最佳答案

Swift 语言指南说:

If you need to give a constant or variable the same name as a reserved Swift keyword, surround the keyword with back ticks (`) when using it as a name. However, avoid using keywords as names unless you have absolutely no choice.

在你的例子中你确实有一个选择......

但是如果你真的真的真的想使用 switch 作为变量名,你必须将 所有 出现的 switch 包含在反引号中。

关于ios - 我收到诸如 "braced block of statements is an unused closure"和预期表达式之类的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33743354/

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