SWIFT2:JSON 请求数组 returns 不同的值

SWIFT2: JSON request to array returns different values

我正在使用 Alamofire 和 SwiftyJSON 将 JSON 数据传递给数组。

第一次打印return一个正确的数组:

[(1, "Arena", "Oklahoma"), (2, "Stafium", "Berlin")]

但是第二个打印一个空数组:

[]

我不明白为什么?

这是我的代码。 由@NickCatib 解决

typealias cType = (ID: Int,Tag: String, Location: String)

var cBlue = [cType]()

var NumRows = 0

class MainViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    Alamofire.request(.GET, url, parameters: ["postType": "live"]).responseJSON { (_, _, result) in

        switch result {

            case .Success(let data):

                let json = JSON(data)

                for(_,subJSON) in json["LocalInfo"] {

                    let ID = subJSON["id"].int!
                    let Tag = subJSON["Tag"].string!
                    let Location = subJSON["Location"].string!

                    let Info = (ID: ID, Tag: Tag, Location: Location)

                    cBlue.append(Info)

                }
                self.tableView.reloadData()

            case .Failure(_, let error):

                print("Request failed with error: \(error)")
        }

    }  

  }

func numberOfSectionsInTableView(tableView: UITableView) -> Int {    
    return 1
}


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
   reloadUI() 
   return cBlues.count   
}

func tableView(tableView: UITableView,
    cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell


 {
        let cell = tableView.dequeueReusableCellWithIdentifier("CellConcert",
            forIndexPath: indexPath)
        let info = cBlue[indexPath.row] as! Info
        cell.textLabel?.text = info.Tag

        return cell
    }
}

这是正确的吗?

谢谢

实际上它非常简单:您的第二个打印将在 Alamofire 请求完成之前执行 - Alamofire.request 是稍后将执行的异步调用。

您将在请求后获得您的信息,如果您需要设置某些 UI 元素,则必须调用某种重新加载视图或 relaodData() 如果您使用 UITableView

您可以在此处调用 reloadUI() 您的自定义函数:

//PRINTS THE ARRAY
reloadUI()
print(cBlue)

示例:

class 主要ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    Alamofire.request(.GET, url, parameters: ["postType": "live"]).responseJSON { (_, _, result) in

        switch result {

            case .Success(let data):

                let json = JSON(data)

                for(_,subJSON) in json["LocalInfo"] {

                    let ID = subJSON["id"].int!
                    let Tag = subJSON["Tag"].string!
                    let Location = subJSON["Location"].string!

                    let Info = (ID: ID, Tag: Tag, Location: Location)

                    cBlue.append(Info)

                }


            case .Failure(_, let error):

                print("Request failed with error: \(error)")
        }
        //PRINTS THE ARRAY
        reloadUI()
        print(cBlue) 

    }  
    //PRINT [] EMPTY ARRAY
    print(cBlue)     
  }
}

func reloadUI(){
    self.tagLabel.text = (cBlue[0] as! Info).tag
    self.locationLabel.text = (cBlue[0] as! Info).location
}