参数类型不符合 Encodable

Argument type does not conform to Encodable

我正在尝试为 POST 请求创建结构。据我所知,该结构符合 Codable 类型别名,但我一直收到错误

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

我试图仅遵循 Encodable,编写建议的必需 init(),但似乎没有任何效果。

这是我的 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"
    }
}

这是我得到的错误

这里需要传递一个符合Codable/Encodable类型的对象,而不是类型本身

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