如何将字符串输入用作数组? Swift
How to use a string input as an array? Swift
我试图将一个数组的结果作为一个字符串,并将这个结果用作一个已经存在的数组的名称。
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 returns CJ,那么我希望能够将CJ作为一个数组来调用。这个问题有道理吗?如果有的话,这可能吗?
Jeep_Model returns "CJ", 然后我想读取数组 CJ.count 或 CJ[2] 等等...
变量名称在编译时计算,您不能在运行时将该名称视为字符串值。
你需要一个字典,键是Jeep_Model
返回的模型,值是关联的数组
对象缩短了一点
let Jeep_Model = ["CJ", "DJ", "SJ"]
let allModels = ["CJ" : ["CJ-1", "CJ-2", "CJ-2A"], "DJ" : ["DJ... "], "SJ" : ["SJ... "]]
现在如果 Jeep_Model
returns CJ
你得到第三个 CJ 模型
let model = allModels["CJ"]![2] // -> "CJ-2A"
我试图将一个数组的结果作为一个字符串,并将这个结果用作一个已经存在的数组的名称。
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 returns CJ,那么我希望能够将CJ作为一个数组来调用。这个问题有道理吗?如果有的话,这可能吗?
Jeep_Model returns "CJ", 然后我想读取数组 CJ.count 或 CJ[2] 等等...
变量名称在编译时计算,您不能在运行时将该名称视为字符串值。
你需要一个字典,键是Jeep_Model
返回的模型,值是关联的数组
对象缩短了一点
let Jeep_Model = ["CJ", "DJ", "SJ"]
let allModels = ["CJ" : ["CJ-1", "CJ-2", "CJ-2A"], "DJ" : ["DJ... "], "SJ" : ["SJ... "]]
现在如果 Jeep_Model
returns CJ
你得到第三个 CJ 模型
let model = allModels["CJ"]![2] // -> "CJ-2A"