简体   繁体   English

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

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

I'm currently writing my first custom framework, however, in my alamofire request, I am not able to pass back my offers. 我目前正在编写我的第一个自定义框架,但是,在alamofire请求中,我无法传递我的报价。 Any advice? 有什么建议吗?

From an app perspective, I am able to append to array in .responseJSON closure then tableView.reloadData() 从应用程序的角度来看,我能够在.responseJSON闭包中附加到数组,然后是tableView.reloadData()

How do I pass back my array created in my for loop in my .responseJSON closure to my view controller that is using this framework? 如何将在.responseJSON闭包中的for循环中创建的数组传递回使用此框架的视图控制器?

Sorry for the newbie question cause this is the first ever framework that I'm writing 对不起,对于新手问题,因为这是我正在编写的第一个框架

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 is as follow getCurrentViewController如下

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

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

}

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

When you are done with the for loop, call the callback function and pass your array back. 完成for循环后,调用回调函数并将数组传递回。

In the view controller now you can do what you want with it (ie append it to your table data array and reload the table, etc) 现在,在视图控制器中,您可以使用它进行所需的操作(即,将其附加到表数据数组中并重新加载表,等等)

When you are presenting any UIViewController then you can pass the data directly. 当您呈现任何UIViewController您可以直接传递数据。 For that, you need to override prepareForSegue method. 为此,您需要重写prepareForSegue方法。 Check out the below example: 查看以下示例:

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

This is how you can pass the data to presenting UIViewController . 这是将数据传递给呈现UIViewController

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

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