繁体   English   中英

从alamofire封闭中的自定义框架传回数组

[英]passing array back from a custom framework in alamofire closure

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

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

如何将在.responseJSON闭包中的for循环中创建的数组传递回使用此框架的视图控制器?

对不起,对于新手问题,因为这是我正在编写的第一个框架

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(callback:((offers:[Offer])-> Void)?){...}

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

现在,在视图控制器中,您可以使用它进行所需的操作(即,将其附加到表数据数组中并重新加载表,等等)

当您呈现任何UIViewController您可以直接传递数据。 为此,您需要重写prepareForSegue方法。 查看以下示例:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "YOUR_CONTROLLER_IDENTIFIER" {
        if let destination = segue.destinationViewController as? YourDestinationController {
            destination.arroffers = offers
        }
    }
}

这是将数据传递给呈现UIViewController

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM