Swift构造多项选择

时间:2016-10-17 02:41:12

标签: ios swift

我需要使用Swift构建一个多选Q& A iOS应用。目前我使用的是“case”结构,如下所示。我面临两个问题:

  1. 一旦正确回答,我不知道如何删除问题(或案例)
  2. 当我构建成百上千个问题时,这个结构变得很麻烦。
  3. 有人可以提供更好的方法来实现这个吗?

    func RandomQuestions () {
        var RandomNumber = arc4random() % 4
    
        switch (RandomNumber) {
    
        case 1:
            QuestionLabel.text = "What is the capital of Austria? "
            Button1.setTitle ("Istanbul", for: UIControlState.normal)
            Button2.setTitle ("Vienna", for: UIControlState.normal)
            Button3.setTitle ("Melbourne", for: UIControlState.normal)
            Button4.setTitle ("Perth", for: UIControlState.normal)
            CorrectAnswer = "2"
    
            break
        case 2:
            QuestionLabel.text = "What is 6 x 7? "
            Button1.setTitle ("12", for: UIControlState.normal)
            Button2.setTitle ("24", for: UIControlState.normal)
            Button3.setTitle ("42", for: UIControlState.normal)
            Button4.setTitle ("48", for: UIControlState.normal)
            CorrectAnswer = "3"
    
            break
        case 3:
            QuestionLabel.text = "What is the name of the yellow Power Ranger? "
            Button1.setTitle ("Willy", for: UIControlState.normal)
            Button2.setTitle ("Mario", for: UIControlState.normal)
            Button3.setTitle ("Ivan", for: UIControlState.normal)
            Button4.setTitle ("Alexander", for: UIControlState.normal)
            CorrectAnswer = "3"
    
            break
        case 4:
            QuestionLabel.text = "How many dwarves in Cinderella"
            Button1.setTitle ("12", for: UIControlState.normal)
            Button2.setTitle ("7", for: UIControlState.normal)
            Button3.setTitle ("3", for: UIControlState.normal)
            Button4.setTitle ("9", for    : UIControlState.normal)
            CorrectAnswer = "2"
    
            break
    
        default:
            break
    

1 个答案:

答案 0 :(得分:1)

我要做的是设计一个简单的模型对象(一个结构),其属性是文本,四个按钮标题和正确的答案。称之为问题。现在,您未回答的问题列表将成为一个问题数组。随机数是该数组的索引。此外,数组可以是可变的,因此您可以从中删除正确回答的问题。