Weebly / Cereal

Swift object serialization
BSD 3-Clause "New" or "Revised" License
369 stars 13 forks source link

question: Encoding an enum with associated types #17

Closed cbowns closed 8 years ago

cbowns commented 8 years ago

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.

ketzusaka commented 8 years ago

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

cbowns commented 8 years ago

Oooooohhhhh, interesting! Thanks!