简体   繁体   中英

Argument type does not conform to Encodable

I am trying to create a Struct for a POST request. The struct conforms, to the best of my knowledge to the Codable typealias but I keep getting the error

Argument type 'RegisterUserRequest.Type' does not conform to expected type 'Encodable' " when passing it in as a parameter to my JSONEncoder .

I have tried to conform only to Encodable , to write the suggested required init() but nothing seems to work.

This is the way my Struct looks like


struct RegisterUserRequest: Codable {
    var firstName: String
    var lastName: String
    var email: String
    var phoneNumber: String
    var dateOfBirth: String

    enum CodingKeys: String, CodingKey {
        case firstName = "first_name"
        case lastName = "last_name"
        case email
        case phoneNumber = "phone"
        case dateOfBirth = "date_of_birth"
    }
}

This is the error I get

在此处输入图片说明

Here you need to pass an object of a type that conforms to Codable / Encodable not the type itself

do {
    let instance = RegisterUserRequest(firstname:////////......
    let data = try JSONEncoder().encode(instance)
}
catch {
  print(error)
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM