我想制作一个小程序,当一个人在文本字段中键入“happy”或“sad”时,标签会根据案例更新并说出一些内容......这是我的代码:
class ViewController: UIViewController {
@IBOutlet weak var aiAnswer: UILabel!
@IBOutlet weak var humanResponse: UITextField!
// when user taps submit run this
@IBAction func responseRequest(sender: AnyObject) {
// check the variable humanResponse to see if they typed happy or sad and update the UI label based on that
switch (humanResponse) {
case "happy":
aiAnswer.text = "thats great your happy!";
case "sad":
aiAnswer.text = "thats no fun!";
default:
aiAnswer.text = "please input a valid emotions";
}
}
答案 0 :(得分:0)
我认为您的switch
案件存在问题。像这样更新:
switch humanResponse.text! { //text entered by user
case "happy":
aiAnswer.text = "thats great your happy!";
case "sad":
aiAnswer.text = "thats no fun!";
default:
aiAnswer.text = "please input a valid emotions";
}