簡體   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