2014-12-19 45 views
0

我已经定义我的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!) 

      } 
     } 

现在从上面的代码如何将我填补二维数组?

+1

我建议你创建一个字典或数组的一些结构数组,并把couCode,couDescription到相同的结构。这样它会简化你的实现。 – Sandeep 2014-12-19 06:39:07

+0

感谢您的真正快速响应.... @sandeep ...我如何创建一个字典的全局变量和存储到它...你能给我一个快速的例子....所以我可以理解。 .sorry我是新来的swift ... – 2014-12-19 06:41:12

回答

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

towDescription在此位置

towCode在位置在分栏列表实测值= description4

towDescription 3

towCode发现在该位置= code4

如果你想在cList中找到多个命中,删除break语句。

+0

谢谢...这使我的工作更容易...当我们检查towDescription时,我想要towcode值而不是(位置3)...我们怎么能得到这个? – 2014-12-19 09:21:58

+0

我这样做...但我得到的描述不是代码.....对于我我:Int = 0;我 2014-12-19 09:25:16

+0

我如何从描述中获得“code1”? – 2014-12-19 10:05:24

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 = [字符串1,字符串],[STRING3,串,4]

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 = [[字符串1,字符串,STRING5],[STRING3,串,4,String6]]

CLIST [0] [0] = String1中

CLIST [0] [1] = String2的

栏列表[0] [2] = STRING5

+0

谢谢....我已经实现了它...但它给了我一个错误...不可变的值类型'[([(String)])]''有变异的成员名为'append'...你有想法吗? – 2014-12-19 07:00:04

+0

那是因为你用let声明它;用var声明它。 var使可变数组。 – jwlaughton 2014-12-19 07:03:16

+0

非常感谢,工作....多一个问题...我现在有两维数组存储...但是,如果我想检查towCode选择的描述比我将如何检查?任何帮助表示赞赏....这是我如何存储...让towCode:字符串! =镇[“towCode”]为?字符串 let towDescription:String! =镇[“towDescription”]为?字符串 self.cList.append([towCode,towDescription]) – 2014-12-19 07:25:14