This is my third post about linked lists in Swift. In the previous post we’ve made our linked list a little bit more „Swifty“. Today I want to introduce some more convenient methods to make life easier.
Swift
Linked Lists in Swift (2)
And here comes my second post in the series about linked lists in Swift. In the first post we have implemented the basics to make our linked list work. Now, let’s make our linked list a little bit more „Swifty“.
Linked Lists in Swift (1)
This is the first post of my upcoming series devoted to linked lists. I like this data structure very much because of its elegance and efficiency in situations where you have to insert and remove objects quite often and randomly. Unfortunately there is no standard data type „Linked List“ in Swift, so we have to build one by ourself. That’s not too difficult and there are quite a few example implementations in the web (e.g. from Chris Pilcher or Hugo Tunius). I’ll take these as a starting point and add some of my own thinkings and optimisations.
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.
Making structs serializable
Just for completeness I’d like to show in this post how to make structs serializable in the same manner as enums (see this post). Again, we transform the struct to a dictionary of NSCoding compliant types and give it to an NSCoder (e.g. NSKeyedArchiver) for coding. The other way round we get the decoded dictionary and initialize the desired struct with it. Sounds simple, doesn’t it. Let’s start.