在swift中向数组添加数据

时间:2015-11-27 09:24:31

标签: arrays swift

我正在尝试将数据附加到Array并使用TMCache进行保存,但这就像我做错了一样。因为,数据没有被追加。我一直在拿空阵列

private var teams: Array<Teams> = Array<Teams>()
private var teamResults: [TeamResult]! {
    didSet {
        if teamResults.count <= 0 {
            return
        } else {
            self.teams = []
            for var index = 0; index < teamResults.count; index++ {
                //print(index)
                let categoryResult = teamResults[index]
                if let categoryBackgroundImage = categoryResult["image"] as? PFFile {
                    categoryBackgroundImage.getDataInBackgroundWithBlock({ (data, error) -> Void in
                        if let dataGot = data {
                            let image = UIImage(data: dataGot)
                            let appendData = Teams(playing: categoryResult["playing"] as! Bool,
                                name: categoryResult["name"] as! String,
                                position: categoryResult["position"] as! Int,
                                image: image!)
                            //print(appendData.position)
                            self.teams.append(appendData)
                        }
                        print(self.teams.count) <-- I get 0
                    })
                    print(self.teams.count) <-- I get 0
                }
            }
            TMCache.sharedCache().setObject(self.teams, forKey: "Teams")
            self.mainTableView.reloadData()
            for categ in teams {
                print(categ.position)
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

getDataInBackgroundWithBlock异步工作。稍后将在块中返回数据。

您必须将代码重新加载到块中并检查循环是否已完成。

例如(未经测试)

private var teams: Array<Teams> = Array<Teams>()
private var teamResults: [TeamResult]! {
  didSet {
    if teamResults.count <= 0 {
        return
    } else {
        self.teams = []
        var index : Int
        for index = 0; index < teamResults.count; index++ {
            //print(index)
            let categoryResult = teamResults[index]
            if let categoryBackgroundImage = categoryResult["image"] as? PFFile {
                categoryBackgroundImage.getDataInBackgroundWithBlock({ (data, error) -> Void in
                    if let dataGot = data {
                        let image = UIImage(data: dataGot)
                        let appendData = Teams(playing: categoryResult["playing"] as! Bool,
                            name: categoryResult["name"] as! String,
                            position: categoryResult["position"] as! Int,
                            image: image!)
                        //print(appendData.position)
                        self.teams.append(appendData)
                        TMCache.sharedCache().setObject(self.category, forKey: "Teams")
                        if index == teamResults.count {
                            self.mainTableView.reloadData()
                            for categ in teams {
                              print(categ.position)
                            }
                        }
                    }
                })
            }
        }
    }
  }
}