条件语句的预期声明

Expected declaration of a conditional statement

我现在设置了将答案放入 4 个选项中的 1 个选项的系统,但出现了一个奇怪的错误:

     var optionAnswer:UInt32 = arc4random_uniform(4)
if optionAnswer == 0 {                    // Expected declaration
   optionA.text = secretAnsarrrrr.text
}
if optionAnswer == 1 {
   optionB.text = secretAnsarrrrr.text
}
if optionAnswer == 2 {
   optionC.text = secretAnsarrrrr.text
}
if optionAnswer == 3 {
   optionD.text = secretAnsarrrrr.text
}

错误只出现在第一个条件上,它没有指定我需要做什么。我该如何解决?

完整代码:

     import UIKit

    class ViewController: UIViewController {

@IBOutlet var numA: UILabel!
@IBOutlet var operation: UILabel!
@IBOutlet var numB: UILabel!
@IBOutlet var secretAnsarrrrr: UILabel!

@IBOutlet var optionA: UILabel!
@IBOutlet var optionB: UILabel!
@IBOutlet var optionC: UILabel!
@IBOutlet var optionD: UILabel!

@IBOutlet var level: UILabel!
@IBAction func startTest(sender: UIButton) {
    var question:Int = 1
    func generateQuestion() {
        var answer:Float = 0.0
        var randomoperation:UInt32 = arc4random_uniform(4)
        if randomoperation == 0 {
            operation.text = "+"
        }
        if randomoperation == 1 {
            operation.text = "-"
        }
        if randomoperation == 2 {
            operation.text = "X"
        }
        if randomoperation == 3 {
            operation.text = "/"
        }
        var randomNumber:UInt32 = arc4random_uniform(1000)
        var randomNumber2:UInt32 = arc4random_uniform(1000)
        // 1000 is my maximum number for now.
        randomNumber += 1
        randomNumber2 += 1
        func identifyVal() {
            if randomNumber < randomNumber2 {
                var between:UInt32 = 1000 - randomNumber2
                randomNumber = randomNumber2 + arc4random_uniform(between - 1)
                //making sure that randomNumber is not smaller than randomNumber2, therefore all results are positive.
            }
        }
        if operation.text == "/" {
            identifyVal()

            answer = round(Float(randomNumber)/Float(randomNumber2))
        }

        if operation.text == "+" {
            answer = Float(randomNumber + randomNumber2)
        }
        if operation.text == "-" {
            identifyVal()
            answer = Float(randomNumber - randomNumber2)
        }
        if operation.text == "x" {
            answer = Float(randomNumber * randomNumber2)
        }
        secretAnsarrrrr.text = "\(answer)"
        numA.text = String(Int(randomNumber))
        numB.text = String(Int(randomNumber2))
    }
    generateQuestion()
}
var optionAnswer:UInt32 = arc4random_uniform(4)
if optionAnswer == 0 {
optionA.text = secretAnsarrrrr.text
}
if optionAnswer == 1 {
optionB.text = secretAnsarrrrr.text
}
if optionAnswer == 2 {
optionC.text = secretAnsarrrrr.text
}
if optionAnswer == 3 {
optionD.text = secretAnsarrrrr.text
}
var correct:Bool?
@IBAction func answerA(sender: UIButton) {
    if optionAnswer == 0 {
        correct = true
    } else {
        correct = false
    }
}
@IBAction func answerB(sender: UIButton) {
    if optionAnswer == 1 {
        correct = true
    } else {
        correct = false
    }
}
@IBAction func answerC(sender: UIButton) {
    if optionAnswer == 2 {
        correct = true
    } else {
        correct = false
    }
}
@IBAction func answerD(sender: UIButton) {
    if optionAnswer == 3 {
        correct = true
    } else {
        correct = false
    }
}

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.
}

}

远景——但是您之前是否删除了该行上的函数?如果是这样,注释掉第一行,然后擦掉“//”。 Xcode 有时会感到困惑。

旁注:使用开关可能效果更好。另外,考虑将它放在一个结构中,这将允许您定义一个对问题起作用的方法 randomAnswer(),然后只需在您的视图中引用该方法。您还可以将不同的选项作为枚举,因为它们只有 4 个。 :)