Update: Serialising enums and structs in Swift 4

So, now Swift 4 is here and things are changing. One of the big improvements of Swift 4 is the incorporation of serialisation for classes, structs and enums. In contrast to the previous versions which rely on the old NSObject/NSCoding mechanisms the new Codable protocol is not restricted to classes only. And even better, if you do not have special needs the compiler will synthesise the needed functions for you as we will see later.

Weiterlesen

Serializing enums with associated values

Welcome to this new posts in my series about enums with associated values. In the previous posts we looked at the equatability of enums with associated values. Another problem arises when we want to store enums (maybe as part of the model) on a file or want to send it over a network, let’s say: want to serialize the enum. Although the primitive types as Int and String conform to the NSCoding protocol as well as collections of those types (e.g. Arrays and Dictionaries) enums and structs don’t. And since they are not classes we can also not implement the NSCoding protocol for them. For simple enums we can use its raw values for coding, but that’s not possible for enums with associated values.

Weiterlesen

Similarity of enums with associated values

If you have read this previous post you have seen how to compare two enums with associated values for equality. The equality included the associated values as well which is only fair. But sometimes you are not interested in the associated values but only want to know, if the two enums are of the same case regardless of the possibly associated values; I call this in the following if they are „similar“.

Weiterlesen