Ios 打印输出结果后执行Alamofire请求
我正在制作一个关于在网站上使用API的应用程序。这是一个火车时刻表应用程序。我在将数据从一个ViewController发送到另一个ViewController时遇到了麻烦。问题是为什么在执行代码“self.trainNo.append(data2.intValue)”和“prepare(…)”时,我创建的全局变量(trainNo)没有传递给另一个VC。 我使用print语句进行调试。我发现当按下按钮时,首先执行Alamofire请求语句中的打印语句“print(self.trainNo)”,然后执行Alamofire请求中的“print(self.trainNo)”。我不知道为什么会这样 我尝试在不同的地方使用打印语句Ios 打印输出结果后执行Alamofire请求,ios,arrays,swift,alamofire,Ios,Arrays,Swift,Alamofire,我正在制作一个关于在网站上使用API的应用程序。这是一个火车时刻表应用程序。我在将数据从一个ViewController发送到另一个ViewController时遇到了麻烦。问题是为什么在执行代码“self.trainNo.append(data2.intValue)”和“prepare(…)”时,我创建的全局变量(trainNo)没有传递给另一个VC。 我使用print语句进行调试。我发现当按下按钮时,首先执行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。
但它不起作用
实际结果:
[//此按钮中的“打印(self.trainNo)”
[]//这是prepare中的“print(self.trainNo)”
[101]
......
[101、3147、371、3157、3167、1]//这些是Alamofire中的“print(self.trainNo)”循环
这意味着Alamofire请求和循环在print语句之后执行,我不知道发生了什么。因为请求是异步的,所以需要在回调中使用
self.performsgue(带标识符)
@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,因此用户希望这是一个网络操作,因为请求是异步的,您需要在回调中使用
self.performsgue(带标识符
)
@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,因此用户希望这是一个网络操作非常感谢@Sh_Khan,它起作用了。我将研究“异步”~~非常感谢@Sh_Khan,它起作用了。我将研究它“异步”~~