gpt4 book ai didi

ios - 在 alamofire 闭包中从自定义框架传回数组

转载 作者:行者123 更新时间:2023-11-29 01:24:36 24 4
gpt4 key购买 nike

我目前正在编写我的第一个自定义框架,但是,在我的 alamofire 请求中,我无法传回我的报价。有什么建议吗?

从应用程序的角度来看,我可以附加到 .responseJSON 闭包中的数组,然后附加到 tableView.reloadData()

如何将 .responseJSON 闭包中的 for 循环中创建的数组传回到使用此框架的 View Controller ?

很抱歉这个新手问题,因为这是我正在编写的第一个框架

public class func getVoucherList() {
Alamofire.request(.GET, "http://surprise.com/api/vouchers", parameters: nil, encoding: .JSON, headers: ["Content-Type": "application/x-www-form-urlencoded", "Authorization": "Token " + token, "accept": "application/vnd.audaxy.com.v1"])
.responseJSON { response in
switch response.result {

case .Success:

let json = JSON(response.result.value!)
let alert = UIAlertController(title: json["status"].string, message: json["message"].string, preferredStyle: .Alert)
let okAction = UIAlertAction(title: "Ok", style: .Cancel, handler: nil)
alert.addAction(okAction)

/*
for surprise in json["surprise"].arrayValue {
offers.append(Offer(offerId: String(surprise["offerid"].intValue), thumbnailUrl: surprise["thumbnail"].string, title: surprise["merchantname"].stringValue, description: surprise["deals"].stringValue, expiryDate: surprise["expire"].stringValue, coverPhotoUrl: surprise["offerimg"].string))
}
*/

VAV.getCurrentViewController().presentViewController(alert, animated: true, completion: {
print(offers)
})
break

case .Failure:
break
}
}

}

getCurrentViewController如下

class func getCurrentViewController() -> UIViewController {
var topViewController = UIApplication.sharedApplication().keyWindow?.rootViewController

while ((topViewController?.presentedViewController) != nil) {
topViewController = topViewController?.presentedViewController
}
return topViewController!

}

最佳答案

定义 getVoucherList(回调:((offers: [Offer])->Void)?){...}

完成 for 循环后,调用回调函数并将数组传回。

现在在 View Controller 中你可以用它做你想做的事(即将它附加到你的表数据数组并重新加载表等)

关于ios - 在 alamofire 闭包中从自定义框架传回数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34170926/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com