比较Swift中的两个版本string

我有两个不同的应用程序版本string(即“3.0.1”和“3.0.2”)。

如何比较这些使用Swift

结束了不得不把我的string转换为NSString:

if storeVersion.compare(currentVersion, options: NSStringCompareOptions.NumericSearch) == NSComparisonResult.OrderedDescending { println("store version is newer") } 

Swift 3

 let currentVersion = "3.0.1" let storeVersion = "3.0.2" if storeVersion.compare(currentVersion, options: .numeric) == .orderedDescending { print("store version is newer") } 

Swift 3版本

 let storeVersion = "3.14.10" let currentVersion = "3.130.10" extension String { func versionToInt() -> [Int] { return self.components(separatedBy: ".") .map { Int.init($0) ?? 0 } } } //true storeVersion.versionToInt().lexicographicallyPrecedes(currentVersion.versionToInt()) 

Swift 2版本比较

 let storeVersion = "3.14.10" let currentVersion = "3.130.10" extension String { func versionToInt() -> [Int] { return self.componentsSeparatedByString(".") .map { Int.init($0) ?? 0 } } } // true storeVersion.versionToInt().lexicographicalCompare(currentVersion.versionToInt()) 

你不需要把它作为NSString。 Swift 3中的string对象只是足够强大的比较下面的版本。

 let version = "1.0.0" let targetVersion = "0.5.0" version.compare(targetVersion, options: .numeric) == .orderedSame // false version.compare(targetVersion, options: .numeric) == .orderedAscending // false version.compare(targetVersion, options: .numeric) == .orderedDescending // true 

但是上面的示例并不包括带有额外零的版本(例如:“1.0.0”和“1.0”)

所以,我在String中使用了各种这些扩展方法来处理使用Swift进行版本比较。 它确实考虑了我说的额外的零,非常简单,并会按照您的预期工作。

 XCTAssertTrue(UIDevice.current.systemVersion.isVersion(lessThan: "99.0.0")) XCTAssertTrue(UIDevice.current.systemVersion.isVersion(equalTo: UIDevice.current.systemVersion)) XCTAssertTrue(UIDevice.current.systemVersion.isVersion(greaterThan: "3.5.99")) XCTAssertTrue(UIDevice.current.systemVersion.isVersion(lessThanOrEqualTo: "13.5.99")) XCTAssertTrue(UIDevice.current.systemVersion.isVersion(greaterThanOrEqualTo: UIDevice.current.systemVersion)) XCTAssertTrue("0.1.1".isVersion(greaterThan: "0.1")) XCTAssertTrue("0.1.0".isVersion(equalTo: "0.1")) XCTAssertTrue("10.0.0".isVersion(equalTo: "10")) XCTAssertTrue("10.0.1".isVersion(equalTo: "10.0.1")) XCTAssertTrue("5.10.10".isVersion(lessThan: "5.11.5")) XCTAssertTrue("1.0.0".isVersion(greaterThan: "0.99.100")) XCTAssertTrue("0.5.3".isVersion(lessThanOrEqualTo: "1.0.0")) XCTAssertTrue("0.5.29".isVersion(greaterThanOrEqualTo: "0.5.3")) 

只要看看,并采取所有你想在我的示例扩展库没有许可证关心。

https://github.com/DragonCherry/VersionCompare

有时,storeVersion的长度不等于currentVersion的长度。 例如,也许storeVersion是3.0.0 ,但是,您修复了一个错误并将其命名为3.0.0.1

 func ascendingOrSameVersion(minorVersion smallerVersion:String, largerVersion:String)->Bool{ var result = true //default value is equal let smaller = split(smallerVersion){ $0 == "." } let larger = split(largerVersion){ $0 == "." } let maxLength = max(smaller.count, larger.count) for var i:Int = 0; i < maxLength; i++ { var s = i < smaller.count ? smaller[i] : "0" var l = i < larger.count ? larger[i] : "0" if s != l { result = s < l break } } return result } 

您使用NSString是正确的方法,但这是一个非基金会尝试乐趣:

 let storeVersion = "3.14.10" let currentVersion = "3.130.10" func versionToArray(version: String) -> [Int] { return split(version) { $0 == "." }.map { // possibly smarter ways to do this $0.toInt() ?? 0 } } storeVersion < currentVersion // false // true lexicographicalCompare(versionToArray(storeVersion), versionToArray(currentVersion)) 

使用Swift 3,应用程序版本string可以使用compare函数与设置为numeric的选项进行比较。

请阅读Apple开发人员文档中有关如何工作的Objective-C示例的“String编程指南” 。

我试图在https://iswift.org/playground

 print("2.0.3".compare("2.0.4", options: .numeric))//orderedAscending print("2.0.3".compare("2.0.5", options: .numeric))//orderedAscending print("2.0.4".compare("2.0.4", options: .numeric))//orderedSame print("2.0.4".compare("2.0.3", options: .numeric))//orderedDescending print("2.0.5".compare("2.0.3", options: .numeric))//orderedDescending print("2.0.10".compare("2.0.11", options: .numeric))//orderedAscending print("2.0.10".compare("2.0.20", options: .numeric))//orderedAscending print("2.0.0".compare("2.0.00", options: .numeric))//orderedSame print("2.0.00".compare("2.0.0", options: .numeric))//orderedSame print("2.0.20".compare("2.0.19", options: .numeric))//orderedDescending print("2.0.99".compare("2.1.0", options: .numeric))//orderedAscending 

希望有所帮助!

如果你喜欢使用库,使用这个,不要重新发明轮子。 https://github.com/zenangst/Versions

你可以使用'String.compare'方法。 使用ComparisonResult来确定您的版本何时大于,等于或小于。

例:

 "1.1.2".compare("1.1.1").rawValue -> ComparisonResult.orderedDescending "1.1.2".compare("1.1.2").rawValue -> ComparisonResult.orderedSame "1.1.2".compare("1.1.3").rawValue -> ComparisonResult.orderedAscending 

写了一个小的Swift 3类来做到这一点:

 class VersionString: NSObject { // MARK: - Properties var string = "" override var description: String { return string } // MARK: - Initialization private override init() { super.init() } convenience init(_ string: String) { self.init() self.string = string } func compare(_ rhs: VersionString) -> ComparisonResult { return string.compare(rhs.string, options: .numeric) } static func ==(lhs: VersionString, rhs: VersionString) -> Bool { return lhs.compare(rhs) == .orderedSame } static func <(lhs: VersionString, rhs: VersionString) -> Bool { return lhs.compare(rhs) == .orderedAscending } static func <=(lhs: VersionString, rhs: VersionString) -> Bool { return lhs.compare(rhs) == .orderedAscending || lhs.compare(rhs) == .orderedSame } static func >(lhs: VersionString, rhs: VersionString) -> Bool { return lhs.compare(rhs) == .orderedDescending } static func >=(lhs: VersionString, rhs: VersionString) -> Bool { return lhs.compare(rhs) == .orderedDescending || lhs.compare(rhs) == .orderedSame } } let v1 = VersionString("1.2.3") let v2 = VersionString("1.2.3") print("\(v1) == \(v2): \(v1 == v2)") // 1.2.3 == 1.2.3: true print("\(v1) > \(v2): \(v1 > v2)") // 1.2.3 > 1.2.3: false print("\(v1) >= \(v2): \(v1 >= v2)") // 1.2.3 >= 1.2.3: true print("\(v1) < \(v2): \(v1 < v2)") // 1.2.3 < 1.2.3: false print("\(v1) <= \(v2): \(v1 <= v2)") // 1.2.3 <= 1.2.3: true