Alamofire中的“额外参数方法”

时间:2018-05-31 09:47:45

标签: swift alamofire

我正在尝试使用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
        }

我对此很新,所以请不要介意我的问题是否有点过分。我在这里遇到过类似的问题,但他们并没有帮我弄清楚我在哪里弄错了:)

2 个答案:

答案 0 :(得分:0)

您在请求中将weightValue作为POST param传递,但不可以。如果您使用JSONEncoding,则传递Parameters对象的类型。

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

答案 1 :(得分:0)

您只能传递字典类型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
        }
    }