Weebly/Cereal

question: Encoding an enum with associated types

cbowns opened this issue · 2 comments

Hi! I've got a fun, maybe simple question for you about using Cereal with an enum:

I've got a small class and an enum, which look like:

enum UploadEnum {
    case Sending
    case Sent
    case Failed(error: NSError)
}

class DataClass {
    let name: String
    let upload: UploadEnum
}

I want to encode DataClass with Cereal, but… I don't know how, because of UploadEnum.

With other enums without associated types, I've encoded the enum's rawValue, and then decoded it with the enum's .init(rawValue: <>) initializer.

Can you think of any simple ways to encode UploadEnum? I can think of some invasive approaches which require changing the enum, but I'm seeking alternatives.

Yep, you basically just need to encode the NSError along with the type information. Hopefully this gist will help ya out!

https://gist.github.com/ketzusaka/6ce4731c8eef9f2c984d

Oooooohhhhh, interesting! Thanks!