2017-05-26 1 views
0

Ich benutze Alamofire und swift 3. Ich möchte die Daten, die ich von Http Anfrage erhalten. Im Moment bekomme ich das mit dem Completion-Handler.Wie das Ergebnis der Alamofire HTTP-Anfrage von einer Methode zurückgegeben werden?

func SendHttpRequest(params: AnyObject, header_obj: [String: String], url:String, http_method:HTTPMethod, completion: @escaping (_ response_value: JSON, _ error_type: String)->()) { 

     Alamofire.request(BASE_URL + url, method: http_method, parameters: (params as! [String : Any]) , encoding: URLEncoding.default, headers: header_obj).responseJSON { (response:DataResponse<Any>) in 

      switch(response.result) { 
      case .success(_): 

       if let jsonValue = response.result.value { 
        let json = JSON(jsonValue) 
        completion(json, "") 
       } 
       break 

      case .failure(_): 
       print(response.result.error!) 
       completion(JSON(response.result.value!), "NO_INT") 
       //"The Internet connection appears to be offline." 
       break 

      } 
     }//Alamofire 

    }//SendHttpRequest 

Aber was ich will, ist zurück, was auch immer das Ergebnis, ich bin durch Http Anfrage bekommen. zB: -

func GetUsers() -> [Users]{ 
    //HERE IT SHOULD EXCUTE THE HTTP REQUEST AND RETURN THE USERS LIST 
} 

Wie kann ich es erreichen? Kann ich erreichen, dass die Methode als Parametertheorie durchgeht?

Antwort

1

Ich bin nicht ganz sicher, wie Ihre JSON strukturiert ist oder wie Ihre Benutzer struct/class aussieht, aber es ist etwas ähnliches wie Sie tun sollten. Wenn ich richtig verstanden habe, was du machen wolltest.

class NetworkingStuff { 
     func SendHttpRequest(params: AnyObject, header_obj: [String: String], url:String, http_method:HTTPMethod, success: @escaping (Any) ->(), failure: @escaping (error?) ->()) { 

       Alamofire.request(BASE_URL + url, method: http_method, parameters: (params as! [String : Any]) , encoding: URLEncoding.default, headers: header_obj).responseJSON { (response:DataResponse<Any>) in 

        switch(response.result) { 
        case .success(_): 

         if let jsonValue = response.result.value { 
          let json = JSON(jsonValue) 
          success(json, "") 
         } 
         break 

        case .failure(_): 
         print(response.result.error!) 
         failure(JSON(response.result.value!), "NO_INT") 
         //"The Internet connection appears to be offline." 
         break 

        } 
       } 
      } 
     } 

Handle Daten:

func GetUsers() -> [Users]? { 
     networkingStuff.sendHttpRequest(params: something, header_obj: dict, url: URL, http_method: Alamofire.post, success: { (responseData: Any) ->() in 
       if let data = responseData as? [String: Any], let users = data["users"] as? [String: Any] { 
        if let userArray = Users(dict: users) { 
         return userArray 
        } else { 
         return nil 
        } 
       } 

     }, failure: { (error: Any?) ->() in 
      return nil 
     }) 
    } 
Verwandte Themen