简体   繁体   English

参数类型不符合可编码

[英]Argument type does not conform to Encodable

I am trying to create a Struct for a POST request. 我正在尝试为POST请求创建一个Struct。 The struct conforms, to the best of my knowledge to the Codable typealias but I keep getting the error 据我所知,该结构符合Codable typealias,但我不断遇到错误

Argument type 'RegisterUserRequest.Type' does not conform to expected type 'Encodable' " when passing it in as a parameter to my JSONEncoder . 将参数类型“ RegisterUserRequest.Type”作为参数传递给我的JSONEncoder时,其类型与预期类型“ Encodable” JSONEncoder

I have tried to conform only to Encodable , to write the suggested required init() but nothing seems to work. 我试图仅遵循Encodable ,以编写建议的必需init(),但似乎没有任何效果。

This is the way my Struct looks like 这就是我的Struct的样子


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 在这里,您需要传递符合Codable / Encodable类型的对象, Encodable不是类型本身

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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