Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing strings with the format "2.0.1", "2.0.09"

I need to get the user's app version number and compare it with the current app version on my server. If the user's app version is lower, then he will get a pop-up to update his app. While doing this, I need to compare the version of the app with the versions that are available. How can I compare the strings which are in the format "2.0.1" and "2.0.09" and get the highest one, in Objective-C?

like image 584
Vijay Avatar asked Aug 10 '11 00:08

Vijay


1 Answers

How about using the compare:options: method of the NSString class?

NSString *v1 = @"2.0.1";
NSString *v2 = @"2.1";

NSComparisonResult result = [v1 compare:v2 options:NSNumericSearch];
if (result == NSOrderedSame || result == NSOrderedDescending) {
    // do
} else {
    // do
}
like image 57
csano Avatar answered Sep 29 '22 02:09

csano