简体   繁体   中英

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. Any advice?

From an app perspective, I am able to append to array in .responseJSON closure then 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?

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

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)?){...}

When you are done with the for loop, call the callback function and pass your array back.

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. For that, you need to override prepareForSegue method. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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