Skip to content

Instantly share code, notes, and snippets.

@tatimagdalena
Created March 19, 2019 18:44
Show Gist options
  • Save tatimagdalena/70319f03523ea107e76e196c9d460b1f to your computer and use it in GitHub Desktop.
Save tatimagdalena/70319f03523ea107e76e196c9d460b1f to your computer and use it in GitHub Desktop.
Comparing two version strings in the format "n.n.n" to see if the first one is outdated
static func isOutdated(thisVersion: String, comparisonVersion: String) -> Bool {
let thisVersionComponents = thisVersion.components(separatedBy: ".")
let comparisonVersionComponents = comparisonVersion.components(separatedBy: ".")
let thisVersionNumbers: [Int] = thisVersionComponents.map { Int($0) ?? 0 }
let comparisonVersionNumbers: [Int] = comparisonVersionComponents.map { Int($0) ?? 0 }
for i in 0 ..< 3 {
if thisVersionNumbers[i] > comparisonVersionNumbers[i] {
return false
}
else if thisVersionNumbers[i] < comparisonVersionNumbers[i] {
print("Outdated version")
return true
}
}
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment