如何使用Alamofire请求?

时间:2016-10-11 04:01:03

标签: swift alamofire

我正在使用Alamofire从网址获取数据。我正在使用completionHandler来完成句柄请求。请求完成后我获得响应数据并重新加载带有响应数据的tableview,但是我在重新加载表视图时遇到错误,因为我的数据是nil,问题不在api它返回的数据,问题是我无法激活我的代码,我把数据放到我的本地变量

func makeRequest(id:Int,sortid:Int,completionHandler: @escaping (_ responseObject:JSON) -> ()) throws{
    var urlString="http://kuda-poydem.kz/apitest.php?act=object&cid=\(id)&sort=1&page=\(page)&limit=\(limit)"
    urlString = urlString.addingPercentEncoding(withAllowedCharacters:NSCharacterSet.urlQueryAllowed)!
     (UIApplication.shared.delegate as! AppDelegate).mgr.request(urlString).responseJSON{ response in
        let value = response.result.value
        completionHandler(JSON(value!))
    }
}

func loadNextItem(id:Int,completionHandler: @escaping (_ responseObject:JSON) -> ()) throws{
    var urlString="http://kuda-poydem.kz/apitest.php?act=object&cid=\(id)&sort=1&page=1&limit=1"
    urlString = urlString.addingPercentEncoding(withAllowedCharacters:NSCharacterSet.urlQueryAllowed)!
    (UIApplication.shared.delegate as! AppDelegate).mgr.request(urlString).responseJSON{ response in
        completionHandler(JSON(response.result.value))
    }
}

func getData(id:Int,sortid:Int){
    UIApplication.shared.isNetworkActivityIndicatorVisible = true
    //indicatorView.isHidden=false
    do{
        try makeRequest(id: id,sortid:sortid){response in
            if !response.isEmpty{
                UIApplication.shared.isNetworkActivityIndicatorVisible = false
                if response["info"][0]["success"].bool!{
                    self.message=response["info"][0]["object"]
                    self.container.isHidden=self.container.isHidden ? false : true
                    self.tableView.isScrollEnabled=self.tableView.isScrollEnabled ? false : true
                    self.tableView.isUserInteractionEnabled=self.tableView.isUserInteractionEnabled ? false : true
                    self.tableView.reloadData()
                }
                else{
                    let err=response["errors"]
                    print(err[0])
                }
            }
        }
    }catch let err as Error{
        print(err.localizedDescription)
        UIApplication.shared.isNetworkActivityIndicatorVisible = false
        container.isHidden=container.isHidden ? false : true
        tableView.isScrollEnabled=tableView.isScrollEnabled ? false : true
        tableView.isUserInteractionEnabled=tableView.isUserInteractionEnabled ? false : true
        //indicatorView.isHidden=true
    }
}

我在那里激活了getDta函数:

 override func viewDidLoad() {
    super.viewDidLoad()

    getData(id: (selectedCategory?["id"].int)!,sortid:selectedSortItem)

}

问题在于:

try makeRequest(id: id,sortid:sortid){response in
        if !response.isEmpty{
            UIApplication.shared.isNetworkActivityIndicatorVisible = false
            if response["info"][0]["success"].bool!{
                self.message=response["info"][0]["object"]
                self.container.isHidden=self.container.isHidden ? false : true
                self.tableView.isScrollEnabled=self.tableView.isScrollEnabled ? false : true
                self.tableView.isUserInteractionEnabled=self.tableView.isUserInteractionEnabled ? false : true
                self.tableView.reloadData()
            }
            else{
                let err=response["errors"]
                print(err[0])
            }
        }
    }

为什么我的代码在completionHandler体中不起作用?

0 个答案:

没有答案