Alamofire中的“调用中的额外参数方法”

Posted

技术标签:

【中文标题】Alamofire中的“调用中的额外参数方法”【英文标题】:`Extra argument method in call` in Alamofire 【发布时间】:2018-05-31 09:47:44 【问题描述】:

我正在尝试使用 Alamofire 版本 3 和 Swift 3 发送以下数据。我得到的错误是 Extra argument method in call。 这是我到目前为止所做的:

struct userGoalServerConnection 
        let WeightsUserGoal_POST = "http://api.umchtech.com:3000/AddWeightsGoal"


    


struct wieghtsUserGoal 
        let id : Int
        let token : String
        let data : [ String : String ]
    


    func syncInitialUserGaolValuesWithServer (userID : Int , userToken : String )
        let serverConnection = userGoalServerConnection()
        let weightValue = wieghtsUserGoal.init(id: userID,
                                               token: userToken,
                                               data: ["weight_initial":"11","weight_end":"11","weight_difference":"11","calories_deficit":"11","difficulties":"11","weight_loss_week":"11","start_date":"2016-12-12","end_date":"2016-12-23","days_needed":"11"])

        Alamofire.request(userGoalServerConnection.WeightsUserGoal_POST, method:.post, parameters: weightValue, encoding: JSONEncoding.default, headers:nil).responseJSON  response in
                print(response.request as Any)  // original URL request
                print(response.response as Any) // URL response
                print(response.result.value as Any)   // result of response serialization
        

我对此很陌生,所以请不要介意我的问题是否有点太菜鸟。我在这里遇到了类似的问题,但他们并没有帮助我弄清楚我在哪里犯了错误:)

【问题讨论】:

您在请求中将 weightValue 作为 post 参数传递,它不可能。如果您使用的是JSONEncoding,则传递Parameters 对象的类型。 【参考方案1】:

您在请求中将weightValue 作为POST 参数传递,这是不可能的。如果您使用的是JSONEncoding,则传递Parameters 对象的类型。

let params: Parameters = [
        "Key1": "value1",
        "key2": "value2"
    ]

【讨论】:

【参考方案2】:

你只能向字典类型传递一个 alamofire 请求,你不能发布一个结构类型。

您可以在 Alamofire 请求中发送[String : String][String : Any]

你可以试试这个方法

 let WeightsUserGoal_POST = "http://api.umchtech.com:3000/AddWeightsGoal"




func Serialization(object: AnyObject)  -> String
    do 
        let stringData = try JSONSerialization.data(withJSONObject: object, options: [])
        if let string = String(data: stringData, encoding: String.Encoding.utf8)
            return string
        
    catch _ 

    
    return "\"element\":\"jsonError\""





func syncInitialUserGaolValuesWithServer (userID : Int , userToken : String )


let data = ["weight_initial":"11","weight_end":"11","weight_difference":"11","calories_deficit":"11","difficulties":"11","weight_loss_week":"11","start_date":"2016-12-12","end_date":"2016-12-23","days_needed":"11"] as! [String : String]
        let dataSerialized = Serialization(object: data as AnyObject)
        let param = ["id" : userID,
                     "token" : userToken,
        "data" : dataSerialized ]


  Alamofire.request(WeightsUserGoal_POST, method: .post, parameters: param ,encoding: JSONEncoding.default, headers: nil).responseJSON  response in
            print(response.request as Any)  // original URL request
            print(response.response as Any) // URL response
            print(response.result.value as Any)   // result of response serialization
        
    

【讨论】:

以上是关于Alamofire中的“调用中的额外参数方法”的主要内容,如果未能解决你的问题,请参考以下文章

包装在静态函数中时,Alamofire 4.0“调用中的额外参数'方法'”

调用 Alamofire swift 4 中的额外参数“方法”

调用中的额外参数“方法”。阿拉莫菲尔

使用方案 Alamofire watchOS 构建工作区 Alamofire 失败

SwiftyJson 和 Alamofire 使用的解码结构

Swift 中 AlamoFire 的 Youtube API