Skip to content

Using non final class Decodable #118

@marcmatta

Description

@marcmatta

I have an issue since i last updated my pods and Himotoki got upgraded to 2.0.0.

I no longer can use DecodedType and I have the following scenario I'd appreciate if you can help me fix.

I am subclassing a class which I need to be decodable like such:

class A: Decodable {
    var type : Int = 0
    var title: String = ""

    init (e: Extractor) {
        type = try! e <| "type"
        title = try! e <| "title"
    }

    static func decode(e: Extractor) throws -> A {
        let type : Int = try e <| "type"
        switch type {
        case 0:
            return B(e: e)
        case 1:
            return C(e: e)
        case 2:
            return A(e: e)
        }
    }
}

class B: A {
    var total : Double = 0.0
    override init(e: Extractor) {
        total = try! e <| "total"
        super.init(e: e)
    }
}

class C: A {
    var link : String = ""
    override init(e: Extractor) {
        link = try! e <| "link"
        super.init(e: e)
    }
}

I now have a compiler error on this line static func decode(e: Extractor) throws -> A which says Method 'decode' in non-final class 'A' must return 'Self' to conform to protocol 'Decodable'

Thank you for your help in advance! Great library 👍 !

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions