思路:
將版本號字符串根據(jù).分割成數(shù)組,再遍歷比較第一位羹饰,第二位……直到判斷出版本號大小來毛秘。
func compareVersion(nowVersion:String,newVersion:String) -> Bool {
let nowArray = nowVersion.split(separator: ".")
let newArray = newVersion.split(separator: ".")
let big = nowArray.count > newArray.count ? newArray.count : nowArray.count
for index in 0...big - 1 {
let first = nowArray[index]
let second = newArray[index]
if Int(first)! < Int(second)! {
return true
}
}
return false
}
調(diào)用
let newVersion = self.compareVersion(nowVersion: "1.1.12", newVersion: "1.2.2")
if newVersion {
print("新版本")
}