我已经在swift中定义了我的数组
let cList = [[String]]()
现在我想在获取响应国家/地区代码和国家/地区描述时填充数组。 所以我可以在提交页面时得到它。
我在下面提到我的回复:
if let results = jsonResult["country"] as? NSArray
{
for country in results { {
let couCode: String? = country["couCode"] as? String
let couDescription: String? = country["couDescription"] as? String
println(couCode,couDescription)
self.countryList.append(couDescription!)
}
}
现在从上面的代码我将如何填充二维数组?
答案 0 :(得分:1)
如果我理解你的情况,对于二维数组:
var cList = [[String]]()
cList.append(["String1", "String2"])
cList.append(["String3", "String4"])
println("cList = \(cList)")
println("cList[0][0] = \(cList[0][0])")
println("cList[0][1] = \(cList[0][1])")
结果:
cList = [[String1,String2],[String3,String4]]
cList [0] [0] = String1
cList [0] [1] = String2
或:
var cList = [[String]]()
cList.append(["String1", "String2", "String5"])
cList.append(["String3", "String4", "String6"])
println("cList = \(cList)")
println("cList[0][0] = \(cList[0][0])")
println("cList[0][1] = \(cList[0][1])")
println("cList[0][2] = \(cList[0][2])")
结果:
cList = [[String1,String2,String5],[String3,String4,String6]]
cList [0] [0] = String1
cList [0] [1] = String2
cList [0] [2] = String5
答案 1 :(得分:1)
这是你正在寻找的吗?
var cList = [[String]]()
cList.append(["code1", "description1"])
cList.append(["code2", "description2"])
cList.append(["code3", "description3"])
cList.append(["code4", "description4"])
let towCode:String = "code4"
let towDescription:String = "description4"
for var i:Int = 0; i < cList.count; i++
{
if towCode == cList[i][0]
{
println("towCode found in cList at location \(i)")
println("towDescription at this location = \(cList[i][1])")
break;
}
}
for var i:Int = 0; i < cList.count; i++
{
if towDescription == cList[i][1]
{
println("towDescription found in cList at location \(i)")
println("towCode found at this location = \(cList[i][0])")
break;
}
}
结果:
在位置3的cList中找到了towCode
此位置的描述= description4
在位置3的cList中找到的towDescription
在此位置找到的towCode = code4
如果要在cList中找到多个匹配,请删除break语句。