HandyJSON icon indicating copy to clipboard operation
HandyJSON copied to clipboard

struct can't have designated initializer.

Open ShaneQi opened this issue 10 years ago • 3 comments

For a struct, if it has any designated initializer, compiler will complaint that: type does not conform to protocol 'HandyJSON'.

For example, this struct can't pass compiler:

struct Animal: HandyJSON {
    var name: String?
    var id: String?
    var num: Int?

    init(name: String?, id: String?, num: Int?) {
        self.name = name
        self.id = id
        self.num = num
    }
}

I'm not quite sure if this is a bug.

ShaneQi avatar Sep 26 '16 17:09 ShaneQi

Any class/struct conform to protocol 'HandyJSON' need an empty initializer. Struct has a default one but when you declare other it'll be overrid, which make the struct do not conform to 'HandyJSON'.

On this situation, you do need to declare an empty one.

struct Animal: HandyJSON {
    var name: String?
    var id: String?
    var num: Int?

    init() {}

    init(name: String?, id: String?, num: Int?) {
        self.name = name
        self.id = id
        self.num = num
    }
}

xuyecan avatar Sep 27 '16 09:09 xuyecan

I'll specify it in the readme later.

xuyecan avatar Sep 27 '16 09:09 xuyecan

That makes sense. And thanks for revising readme.

ShaneQi avatar Sep 27 '16 13:09 ShaneQi