更新时间: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)
}