Here, Swift 4.1 automatically generates the == function required for the Equatable conformance, which is really really nice. However, we might also like to make it Comparable so that we could compare the app version to a given version we create in code.
Initially my implementation of this went something like the following:
This gave me a base taste in my mouth because there’s a lot of comparisons that are happening that aren’t immediately obvious. Surely Swift has a better way?!
And of course it does. After attempting (and failing) to create an array of values to compare to each other, I tried putting each of the sub-version values into a tuple and comparing the tuples instead, which surprised me when it worked!
You can compare two tuples if they have the same type and the same number of values. Tuples are compared from left to right, one value at a time, until the comparison finds two values that aren’t equal. Those two values are compared, and the result of that comparison determines the overall result of the tuple comparison. If all the elements are equal, then the tuples themselves are equal.