Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare two version strings in Swift

Tags:

ios

swift

I have two different app version Strings (i.e. "3.0.1" and "3.0.2").

How can compare these using Swift?

like image 297
codeman Avatar asked Jan 13 '15 22:01

codeman


People also ask

Can you compare strings in Swift?

In Swift, you can check for string and character equality with the "equal to" operator ( == ) and "not equal to" operator ( != ).

How do you check if two strings are the same in Swift?

To check if two strings are equal in Swift, use equal to operator == with the two strings as operands. The equal to operator returns true if both the strings are equal or false if the strings are not equal.


1 Answers

Ended up having to convert my Strings to NSStrings:

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") } 
like image 88
codeman Avatar answered Oct 04 '22 12:10

codeman