使用Alamofire通过HTTP发布结构可编码的JSON数组

时间:2019-04-19 10:51:13

标签: swift http alamofire codable

我正在使用 Swift 4.2 Xcode 10.2.1 Alamofire 4.8.2

注意:我曾经使用过Alamofire 5,但还没有文档

我有这个编码:

struct User: Codable {


    var name : String
    var lastname : String
    var email : String
    var passwd : String
    var grade: String
    var img: String
}

然后我为每个变量分配一个nameField值。

       let user = User(name: nameField.text!, lastname: lastnameField.text!, email: emailField.text!, passwd: passwdField.text!, grade: gradeField.text!, img: base64ImageString)

我将此提交给我的职能部门,   submitRegistration(for: user)

然后使用 Alamofire


func submitRegistration(for user: User) {

    Alamofire.request("https://httpbin.org/post", method: .post, User: user, encoding: JSONEncoding.default)
}

但是随后出现以下错误:

  

对成员'request(_:method:parameters:encoding:headers :)'的歧义引用

1 个答案:

答案 0 :(得分:0)

这对我有用

Ambiguous reference to member 'upload(_:to:method:headers:interceptor:)'

或尝试此方法

func postJSON() {

    let url = "Your URL"

    let parameters = ["name":tFname.text!,"Lastname":tFLastname.text!,"password":tFpassword.text!,"email":tFemail.text!,"address":tVaddress.text!] as NSDictionary

    AF.request("http://XXXXXXXXXXXXXXX/register", method: .post, parameters: (parameters as! Parameters), encoding: JSONEncoding.default)
        .responseJSON { response in
            print(response)
            switch response.result {
            case .success:
                print(response)
            case .failure(let error):
                print(error)
            }
    }

}