Following what we discussed in comment. You could do something like that :
import UIKit
struct Version: Comparable {
let major: Int
let minor: Int
let patch: Int
init(versionString: String) {
let categories = versionString.components(separatedBy: ".");
major = Int(categories[0]) ?? 0
minor = (categories.count > 1) ? Int(categories[1]) ?? 0 : 0
patch = (categories.count > 2) ? Int(categories[2]) ?? 0 : 0
}
func asString() -> String {
return "\(major).\(minor).\(patch)"
}
static func < (lhs: Version, rhs: Version) -> Bool {
if (lhs.major < rhs.major) { return true }
if (lhs.minor < rhs.minor) { return true }
if (lhs.patch < rhs.patch) { return true }
return false;
}
}
let versionA = Version(versionString: "16")
let versionB = Version(versionString: "16.0")
versionA == versionB
This gives you full control over your versions.
Edit: tried a bit more and found ou the string compare didn't work in this case, please use the new comparison.
What's missing in this code if you want to go further:
- error handling when the user passes a string that is not a version string
- could do an extension directly in string like
asVersion() which returns a Version that you can compare straight away