loganwright / Genome

A simple, type safe, failure driven mapping library for serializing JSON to models in Swift 3.0 (Supports Linux)
MIT License
762 stars 58 forks source link

Error: method 'newInstance(_:context:)' in non-final class 'ChecklistItem' must return `Self` to conform to protocol 'JsonConvertibleType' #62

Closed jefferythomas closed 8 years ago

jefferythomas commented 8 years ago

I'm getting this error using Carthage install: github "LoganWright/Genome" "2.0.4"

Here is the source code

class ChecklistItem: MappableObject {

    var title: String
    var checked: Bool

    init(title: String, checked: Bool) {
        self.title = title
        self.checked = checked
    }

    convenience init(title: String) {
        self.init(title: title, checked: false)
    }

    required init(map: Map) throws {
        self.title = try map.extract("title")
        self.checked = try map.extract("checked")
    }

    func sequence(map: Map) throws {
        try title ~> map["title"];
        try checked ~> map["checked"];
    }

}

extension ChecklistItem: Equatable { }
func == (lhs: ChecklistItem, rhs: ChecklistItem) -> Bool {
    return lhs.checked == rhs.checked && lhs.title == rhs.title
}
loganwright commented 8 years ago

Hi @jefferythomas

That issue arises from inheritance with classes and protocols getting crossed a bit due to use of Self.

Here's the section that addresses this:

https://github.com/LoganWright/Genome#inheritance

The other option is to move to a struct.

Let me know if you don't get it with that, and we can walk through it together 👍