Arrays 如何在swift 3中使用二维阵列?

Arrays 如何在swift 3中使用二维阵列?,arrays,string,for-loop,multidimensional-array,swift3,Arrays,String,For Loop,Multidimensional Array,Swift3,为什么这个代码不起作用?? 我有一个名为answers的数组,它是一个包含字符串数组集合的数组。 我试图在这里访问由answers[count]表示的“answers”数组中的单个数组。然后使用via下标访问该子数组中的每个字符串值 错误消息:“下标”不可用。无法使用Int为字符串下标 func showQuestionAndAnswer( b0: RoundedButton, b1: RoundedButton, b2: RoundedButton, b3: RoundedButton, co

为什么这个代码不起作用?? 我有一个名为answers的数组,它是一个包含字符串数组集合的数组。 我试图在这里访问由answers[count]表示的“answers”数组中的单个数组。然后使用via下标访问该子数组中的每个字符串值

错误消息:“下标”不可用。无法使用Int为字符串下标

func showQuestionAndAnswer( b0: RoundedButton, b1: RoundedButton, b2: RoundedButton, b3: RoundedButton, count: Int) -> Void {
    // When a question is answered, count += 1, such that my questionsLabel will show next question in questions array, and buttons will show possible answers to choose from.

    // Set label to proper question.
    questionLabel.text = questions[count]

    // answers is of type [ [String], [String]...]
    for subArray in answers[count] {
        // answers[count] tells me which array of answers I should look into.
        // subArray should be a single array with string values.

            // Each sub array has a unique number of string values. Each value should be placed as title for resective button.
            switch subArray.count {
                case 4 :
                    b0.titleLabel?.text
                        = subArray[0]
                    b1.titleLabel?.text = subArray[1]
                    b2.titleLabel?.text = subArray[2]
                    b3.titleLabel?.text = subArray[3]
                break

                case 3 :
                    b0.titleLabel?.text = subArray[0]
                    b1.titleLabel?.text = subArray[1]
                    b2.titleLabel.text = subArray[2]
                break

                case 2 :
                    b0.titleLabel?.text = subArray[0]
                    b1.titleLabel?.text = subArray[1]

                break

                default:
                b0.titleLabel?.text = "No Answers"
            }
    }

}

答案必须是[[String]]才能打电话。请放心

你也需要打电话

for subArray in answers{
}

答案必须是[[String]]才能打电话。请放心

你也需要打电话

for subArray in answers{
}

正如您所说,这里的答案是字符串数组的数组,这意味着它类似于
var answers:[[string]]

所以你可以像下面这样迭代

for subArray in answers {
  //here subArray would of type [String]
  switch subArray.count{
    //Your code
  }
}

PS.Alexander关于中断语句的看法是正确的。

正如您所说,这里的答案是字符串数组的数组,这意味着它类似于
var answers:[[string]]

所以你可以像下面这样迭代

for subArray in answers {
  //here subArray would of type [String]
  switch subArray.count{
    //Your code
  }
}

PS.Alexander关于break语句的看法是正确的。

您不需要(也不应该)在Swift中明确地放入
break
。案例总是会破裂,除非他们被明确告知
fallthrough
。那么
问题的声明在哪里呢
?如果
答案是
[[String]]
,那么
答案[count]
[String]
,那么
子数组是
字符串,而不是数组。你不需要这样做(而且不应该)在Swift中明确地把
中断
。除非明确地告诉他们
出错
,否则案例总是中断的。那么
问题的声明在哪里呢
如果
答案
是一个
[[String]]
,那么
答案[count]
就是一个
[String]
,因此
子数组
是一个
字符串
,而不是数组。@Alexander:谢谢你的休息提示。回答和问题的声明不包括在这个片段中。问题是,如果我将子数组打印到控制台,它将提供数组中的所有字符串值。我只是无法通过Subscribe单独访问值如果像上面提到的那样使用for循环会发生什么?Hamish是对的。我只是将所需的数组赋给一个常量。我使事情变得比必须的更困难。例如:让子数组=答案[计数]…哈哈。感谢大家的帮助。@Alexander:谢谢你们提供的关于休息的提示。这个片段中没有包含答案和问题的声明。问题是,如果我将子数组打印到控制台,它将提供数组中的所有字符串值。我只是无法通过下标单独访问值。如果使用for会发生什么-如上所述的循环?Hamish是对的。我只是将所需的数组赋给一个常量。我使事情变得比必须的更困难。示例:let subArray=answers[count]…哈哈。感谢大家的帮助。