Weekly Swift articles, podcasts and tips by John Sundell.

Equatable and Hashable structures

Published on 04 Apr 2018

Now that the Swift compiler automatically synthesizes Equatable and Hashable conformances for value types, it's easier than ever to setup model structures with nested types that are all Equatable and Hashable!

typealias Value = Hashable & Codable

struct User: Value {
    var name: String
    var age: Int
    var lastLoginDate: Date?
    var settings: Settings
}

extension User {
    struct Settings: Value {
        var itemsPerPage: Int
        var theme: Theme
    }
}

extension User.Settings {
    enum Theme: String, Value {
        case light
        case dark
    }
}