" SWIFT"字符串到名称数组

时间:2017-08-29 00:26:47

标签: swift

要命名数组的字符串?

var listArr = ["arr1","arr2","arr3","arr4"]
var arr1 = ["aa","bb","cc","dd"]
var arr2 = ["aaw","aww","awwww"]


let test1 = listArr[0]  // get "arr1" - string
test1[2]        // ??? need "cc"

//(好的,其他方式

vet test2 = listArr [0] +" 2"

test2 [0] // ???需要" aaw"

2 个答案:

答案 0 :(得分:1)

使用字典。

var listArr = ["arr1","arr2","arr3","arr4"]
arrays = {"arr1": ["aa", "bb"], "arr2": ["aaa", "bbb"]}

let test1 = arrays[listArr[0]]

答案 1 :(得分:-1)

如果您想将数组加入其中,然后想要访问该元素,那么您可以像这样使用flatMap

var arr1 = ["aa","bb","cc","dd"]
var arr2 = ["aaa","aaa","aaa"]
var arr3 = ["ddd","ddd","ddd"]
var arr4 = ["ttt","ttt","ttt","ttt"]
let listArr = [arr1,arr2,arr3,arr4].flatMap{$0}//Output will be ["aa", "bb", "cc", "dd", "aaa", "aaa", "aaa", "ddd", "ddd", "ddd", "ttt", "ttt", "ttt", "ttt"]
for str in listArr {
  print(str)
}