我该如何解决“无法为索引类型为UInt32的值[[[String]]”下标”

时间:2019-02-10 03:10:21

标签: swift xcode9

我对此UInt32有疑问, 我的按钮中的“答案”出现了以下错误:“无法为索引为[[String]]的类型为UInt32的下标”

let answers = [["1. After the exam, I felt too exhausted and famished to eat my foods.","2. I could eat a horse, I am a famish now.","3. I famished my stomach next time you treat me to a meal out.","4. I will bring lots of pizza, that's famish."],["Would","Has to","Must","Could"]]  

var rightanswerplacement:UInt32 = 0


 rightanswerplacement = arc4random_uniform(2)+1
        var button:UIButton = UIButton()

        var x = 1

        for i in 1...3{

                button = view.viewWithTag(i) as! UIButton

            if (i == Int(rightanswerplacement)){
                button.setTitle(answers[rightanswerplacement][0], for: UIControlState.normal)

            }
            else{
                button.setTitle(answers[rightanswerplacement][x], for: UIControlState.normal)
                x = 2
            }

            currentquestions += 1

        }

1 个答案:

答案 0 :(得分:0)

Int 用于下标。

注意:我删除了视图以测试Int的正确性。

let answers = [["1. After the exam, I felt too exhausted and famished to eat my foods.","2. I could eat a horse, I am a famish now.","3. I famished my stomach next time you treat me to a meal out.","4. I will bring lots of pizza, that's famish."],["Would","Has to","Must","Could"]]

var rightanswerplacement: Int = 0
rightanswerplacement = Int(arc4random_uniform(2)) + 1
var x = 1
for i in 1...3 {
    if (i == Int(rightanswerplacement)) {
        print(answers[rightanswerplacement][0])
    } else {
        print(answers[rightanswerplacement][x])
        x = 2
    }
}

输出

Would
Has to
Must