且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Swift 4-无法使用类型为((Codable)'的参数列表调用'encode'

更新时间:2022-05-16 17:55:33

您需要将具体类型传递给JSONEncoder.encode,因此您需要在Encodable(Codable不需要,因为它太严格了.)

You need a concrete type to be passed into JSONEncoder.encode, so you need to make your function generic with a type constraint on Encodable (Codable is not needed, its too restrictive).

func encodeRequestJSON<T:Encodable>(apiRequestObject: T) throws -> Data {
    return try JSONEncoder().encode(apiRequestObject)
}