打印输出结果后执行Alamofire请求

时间:2019-01-21 10:25:11

标签: ios arrays swift alamofire

我正在制作一个有关在网站上使用API​​的应用程序。这是火车时刻表应用程序。而且我在将数据从一个ViewController发送到另一个ViewController时遇到麻烦。问题是为什么执行代码“ self.trainNo.append(data2.intValue)”和“ prepare(...)”时,我创建的全局变量(trainNo)没有传递给另一个VC。 我使用print语句进行调试。我发现按下按钮时,首先执行Alamofire请求语句中的打印语句“ print(self.trainNo)”,然后执行Alamofire请求中的“ print(self.trainNo)”。我不知道为什么会这样。

我尝试在其他地方使用打印语句。

@IBAction func buttonPressed(_ sender: Any) {


    APIUrl = "https://ptx.transportdata.tw/MOTC/v2/Rail/TRA/DailyTimetable/OD/"+"\(startStationNo)"+"/to/"+"\(endStationNo)"+"/"+"\(selectedDate)"+"?$top=30&$format=JSON&$orderby=OriginStopTime/ArrivalTime"

    let request = setUpUrl(APIUrl: APIUrl)

    print("\(APIUrl)")

    Alamofire.request(request).responseJSON { response in
        do{
            let json: JSON = try JSON(data: response.data!)

            if let result = json.array {
                for data in result {

                    let data2 = data["DailyTrainInfo"]["TrainNo"]
                    self.trainNo.append(data2.intValue)
                    print(self.trainNo)

                }
            }
            else{
                print("ERROR in data2")
            }

        }
        catch{
            print("ERROR in json \(error)")
        }
    }
    print(self.trainNo)

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.destination is TimeTableController {
        let vc = segue.destination as? TimeTableController
        vc?.trainNo = self.trainNo
        print(self.trainNo)
    }
}

我希望trainNo的值将被更改并传递给另一个VC。 但这没用。

实际结果:

[] //这是按钮中的“ print(self.trainNo)”

[] //准备中的这个是“ print(self.trainNo)”

[101] ......     [101,3147,371,3157,3167,1] //这些是“ print(self.trainNo)” Alamofire中的循环

这意味着Alamofire请求和循环在打印语句后执行,我不知道发生了什么。

1 个答案:

答案 0 :(得分:2)

由于请求是异步的,因此您需要在回调中使用self.performSegue(withIdentifier

@IBAction func buttonPressed(_ sender: Any) {


    APIUrl = "https://ptx.transportdata.tw/MOTC/v2/Rail/TRA/DailyTimetable/OD/"+"\(startStationNo)"+"/to/"+"\(endStationNo)"+"/"+"\(selectedDate)"+"?$top=30&$format=JSON&$orderby=OriginStopTime/ArrivalTime"

    let request = setUpUrl(APIUrl: APIUrl)

    print("\(APIUrl)")

    Alamofire.request(request).responseJSON { response in
        do{
            let json: JSON = try JSON(data: response.data!)

            if let result = json.array {
                for data in result {

                    let data2 = data["DailyTrainInfo"]["TrainNo"]
                    self.trainNo.append(data2.intValue)
                    print(self.trainNo)

                }

                // this will trigger the segue
                self.performSegue(withIdentifier: "segueName", sender: nil)
            }
            else{
                print("ERROR in data2")
            }

        }
        catch{
            print("ERROR in json \(error)")
        }
    }
    print(self.trainNo)

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.destination is TimeTableController {
        let vc = segue.destination as? TimeTableController
        vc?.trainNo = self.trainNo
        print(self.trainNo)
    }
}

并将segue的源连接到vc本身而不是连接到按钮,顺便说一句,您可能还需要在请求之前将活动指示器显示为良好的UX,因此用户希望这是网络操作