将String转换为数组引用

时间:2017-08-11 11:06:13

标签: swift

我有多个数组, 第一个包含所有其他数组的名称。

所以我检索第一个数组的值我得到了我想要的数组的名称,但我不知道如何从那里开始。由于swift将我的结果视为字符串,而不是变量名称。我在下面重新创建了我的问题

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

我得到的错误是:

  

&#39;下标&#39;不可用:不能使用Int

下标String

2 个答案:

答案 0 :(得分:1)

您正在尝试从字符串中间值等于&#34; trait3&#34;在你的情况下。但是,在您的情况下,无法进行字符串订阅。你可以尝试这样的事情:

var trait1 = ["trait 0","trait 1","trait 2","trait 3","trait 4"]
var trait2 = ["final Answer 0","final Answer 1","final Answer 2","final Answer 3","final Answer 4"]
var trait3 = ["final Answer 0","final Answer 1","final Answer 2","final Answer 3","final Answer 4"]

var traits = [trait1, trait2, trait3]

var counter = 2

let intermediateV = traits[counter]

print(intermediateV[2])

答案 1 :(得分:1)

如果你不能做多维数组,你必须使用词典:

var traitKeys = ["trait one", "trait 2", "trait3"] // downloaded from somewhere, dynamic

var traits = [
    "trait one" : ["final Answer 0","final Answer 1","final Answer 2","final Answer 3","final Answer 4"], 
    "trait 2" : ["final Answer 0","final Answer 1","final Answer 2","final Answer 3","final Answer 4"], 
    "trait3" : ["final Answer 0","final Answer 1","final Answer 2","final Answer 3","final Answer 4"]
]

let counter = 2
let traitKey = traitKeys[counter]

print(traits[traitKey] ?? "trait not found")