从JSON设置按钮标题

时间:2015-07-06 07:19:08

标签: ios json swift

我正在遍历我的 C1 C2 C3 1 1 0 0 2 0 0 0 3 1 0 0 4 0 1 3 5 0 1 3 数据,并希望根据我的JSON属性标题设置按钮标题我该怎么办?

JSON

这就是我尝试的错误,我收到的错误是:func getDataFromJSON(){ //Calling getCollectionViewData from the RestParser class RestParser.sharedInstance.getConfig{json in //iterating to the JSON file to get alle data let results = json[0]["menu"] for (index: String,cofigData:JSON)in results{ self.items.append(cofigData) } let testString = self.items[0]["title"] self.btnCollectionView.setTitle(testString.stringValue, forState: UIControlState.Normal) println(testString.stringValue) } }

2 个答案:

答案 0 :(得分:0)

发生错误是因为items显然是一个数组,而密钥订阅items["title"]需要一个字典。

答案 1 :(得分:0)

itemsarray,您必须将其转换为Dictionary

然后,执行此操作:

func getDataFromJSON(){
    //Calling getCollectionViewData from the RestParser class
    RestParser.sharedInstance.getConfig{ json in
        //iterating to the JSON file to get alle data
        let results = json[0]["menu"]
        for (index: String,cofigData:JSON)in results{
          self.items[index] = cofigData
        }
        let testString: String = items["title"]
        self.btnCollectionView.setTitle(testString, forState: UIControlState.Normal)
    }
}

注意:关闭时,reference cycle警告自我。我认为你应该像这样添加[unowned self]

RestParser.sharedInstance.getConfig{ [unowned self] json in
        //iterating to the JSON file to get alle data
  

如果捕获的参考永远不会变为零,则应始终为零   被捕获为无主参考,而非弱参考。

文档here有关引用计数

相关问题