比较Swift中"版本控制"字符串的最佳方法

Mat*_*oal 2 swift

我需要比较代表版本的2个字符串,比如1.2.42.3.6.我想知道在swift中是否已经存在一些类似的实现.也许类似于systemInfo函数的东西UIDevice.如果没有,您认为这是执行此检查的好方法吗?

vad*_*ian 9

一个好方法是compare:带选项的方法.NumericSearch

let version1 = "1.2.4"
let version2 = "2.3.6"

let result = version1.compare(version2, options: .numeric)
switch result {
case .orderedSame : print("versions are equal")
case .orderedAscending : print("version1 is less than version2")
case .orderedDescending : print("version1 is greater than version2")
}
Run Code Online (Sandbox Code Playgroud)