我试图将一个数组的结果作为字符串,并将此结果用作已存在数组的名称。
var Jeep_Model: [String] = ["CJ",
"DJ",
"SJ",
"XJ",
"Comanche",
"YJ",
"TJ",
"JK"
//"Grand Cherokee",
//"Renegade",
//"Compass",
//"Commander",
//"Liberty",
//"Patriot",
//"Wagoneer"
]
var CJ: [String] = ["CJ-1",
"CJ-2",
"CJ-2A",
"CJ-V35",
"CJ-4",
"CJ-4M",
"CJ-4MA",
"CJ-3B",
"CJ-5: Tuxedo Park Mark III",
"CJ-5: Camper",
"CJ-5: 462",
"CJ-5: Renegade I",
"CJ-5: Renegade II",
"CJ-5: Super Jeep",
"CJ-5: Golden Eagle",
"CJ-5: Golden Eagle California Edition",
"CJ-5: Golden Hawk",
"CJ-5: Silver Anniversary",
"CJ-6",
"CJ-3B Long",
"CJ-5A",
"CJ-6A",
"CJ-7",
"CJ-5",
"CJ-8: Scrambler",
"CJ-10"
]
因此,如果Jeep_Model返回CJ,那么我希望能够将CJ称为数组。这个问题是否有意义,如果是这样,这甚至可能吗?
Jeep_Model返回" CJ",然后我想读取数组CJ.count或CJ [2]等等......
答案 0 :(得分:1)
在编译时评估变量名称,您无法在运行时将名称视为字符串值。
你需要一个字典,关键是从Jeep_Model
返回的模型,该值是相关的数组。
对象缩短了一点
let Jeep_Model = ["CJ", "DJ", "SJ"]
let allModels = ["CJ" : ["CJ-1", "CJ-2", "CJ-2A"], "DJ" : ["DJ... "], "SJ" : ["SJ... "]]
现在,如果Jeep_Model
返回CJ
,您将获得第三个带有
let model = allModels["CJ"]![2] // -> "CJ-2A"