使用HTTP发布一个struct Codable作为带有Alamofire的JSON数组

问题描述 投票:0回答:1

我使用的是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)
}

但后来我收到以下错误:

对成员请求的不明确引用(_:method:parameters:encoding:headers :)'

swift http alamofire codable
1个回答
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)
            }
    }

} 
© www.soinside.com 2019 - 2024. All rights reserved.