ios – Protocol with Encodable protocol


I’ve struct that has parameter with protocol kind

protocol CheckoutOrder {}

struct CheckoutOrderEntity: Encodable {
    non-public enum CodingKeys: String, CodingKey {
        case quantity
        case fee
    }
    
    let quantity: Int
    let fee: CheckoutOrder
}

Even have 2 structs which implements protocol CheckoutOrder

struct ApplePayOrderEntity: Encodable {
    non-public enum CodingKeys: String, CodingKey {
        case token
    }
    
    let token: ApplePayTokenEntity
}

extension ApplePayOrderEntity: CheckoutOrder { }
struct CheckoutPaymentEntity: Encodable {
    non-public enum CodingKeys: String, CodingKey {
        case terminalName
        case shouldUseBalance
    }
    
    let shouldUseBalance: Bool
    let terminalName: String
}

extension CheckoutPaymentEntity: CheckoutOrder { }

I’ve an error: Sort ‘CheckoutOrderEntity’ doesn’t conform to protocol ‘Encodable’ and CheckoutOrder protocol requires operate ‘encode(to:)’…. So, the primary purpose of utilizing protocol is that fee may be kind of ApplePayOrderEntity or CheckoutPaymentEntity. Methods to remedy downside or what’s finest observe on this case?

Leave a Reply