Genericize method to encode a `Codable` protocol

abcross92
import Foundation

protocol ProtocolA: Codable {
    var var1: String { get }
}

struct Type1: ProtocolA {
    var var1: String
    var var2: Int
}

struct Type2: ProtocolA {
    var var1: String
    var var2: Bool
}

func encode<T: ProtocolA & Encodable>(object: T) throws -> Data {
    return try JSONEncoder().encode(object as! T.Type)
}

Putting the above in a playground results in error: argument type 'T.Type' does not conform to expected type 'Encodable'

Why does this happen when I am saying that T has to conform to Encodable?

Rob Napier
return try JSONEncoder().encode(object as! T.Type)

This means to convert object to the metatype of T. The type of the type. For example, 1 is an Int. But "Int" itself has a type, which is Int.Type, which is a metatype. Metatypes do not conform to Encodable.

You meant:

return try JSONEncoder().encode(object as! T)

But you really just meant:

return try JSONEncoder().encode(object)

since object is always of type T. That's its explicit type from the function signature. Since you also don't rely on ProtocolA for this algorithm, this all boils down to:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I call `didSet` method using `Codable` protocol

App crashes when trying to encode custom objects that conform to codable protocol and I can't figure out why, any thoughts?

A codable structure contains a protocol property

does not conform to protocol Decodable / Codable

Networking using JSONDecoder & codable protocol

Swift Codable protocol with recursive enums

Parsing Json with Decoding Protocol (Codable)

How to use Codable encode with object inside object

Using JSONEncoder to encode a variable with Codable as type

Swift Codable - How to encode custom array

How to save an enum conforming to Codable protocol to UserDefaults?

Swift Codable protocol… encoding / decoding NSCoding classes

Protocol extending Encodable (or Codable) does not conform to it

Is it possible to make an `any Protocol` conform to Codable?

Codable class does not conform to protocol Decodable

Use Codable Protocol on Custom Classes in SpriteKit

How to use Alamofire with Codable protocol and parse data

Managing Dynamic Keys in response through Codable Protocol

How to use Codable protocol with reference types?

How to use CodingKeys for enums conforming to Codable Protocol?

Decoding only part of a JSON using Codable protocol

Swift Codable Protocol. String encoding issue

"How to use Codable Protocol for a Networking Layer "

Codable class doesn't conform to protocol 'Decodable'

How can I use Swift’s Codable to encode into a dictionary?

Swift: How to pass a codable type as a function input to encode or decode json

Swift Codable: How to encode top-level data into nested container

Swift Codable - how to encode and decode stringified JSON values?

Swift 4 - Cannot invoke 'encode' with an argument list of type '(Codable)'