1

I know this isn't going to be a challenging exercise, but I thought I'd ask, just in case there's some methods already in objective-c etc.

In my app I only handle a 2 number version number e.g. 1.5

I want to upgrade this to 4 numbers which could have up to 4 digits.

So I need to handle existing numbers and return true of false when passed database version and the bundle version numbers.

At the moment I simply do

NSString *strOnePointFive = @"1.5";
if (dblDBVersion < [strOnePointFive doubleValue]) {

}
Jules
  • 7,568
  • 14
  • 102
  • 186
  • you can handle in strings, instead of int, float or double.. – vishy Sep 06 '12 at 13:11
  • @jules, when you say 4 digits, do you mean for example 1.5.2.3? Or do you mean 4 sets of digits for example 1.5.23.067? – Black Frog Sep 06 '12 at 13:23
  • @BlackFrog I mean the number could be 9999.9999.9999.9999 to be complete but commonly could be 1.9.1.1415 – Jules Sep 06 '12 at 14:36
  • Note that your example is not correct for even 2-part numbers: Compare version "1.5" to version "1.45". – Hot Licks Sep 06 '12 at 15:40
  • Yes your right, hmmm, I guess that would need to be considered. Not as straight forward as it seems. – Jules Sep 06 '12 at 15:54

2 Answers2

1

This is a duplicate, was in other formats. Here are some answers to how to handle the version numbers, either equal, greater or below the required version number. Which is answer in this link..

NSString *reqSysVer = @"3.1";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)
    isSupported = YES;
else
    isSupported = NO;
Community
  • 1
  • 1
vishy
  • 3,241
  • 1
  • 20
  • 25
0

What I do is take that string and break it into components:

NSArray *array = [myVersion componentsSeparatedByCharactersInSet:@"."];

NSInteger value = 0;
NSInteger multiplier = 1000000;
for(NSString *n in array) {
  value += [n integerValue] * multiplier;
  multiplier /= 100;
}

What this does is give you a normalized value you can use for comparison, and will generally compare releases that have different "depths", ie 1.5 and 1.5.2.

It breaks if you have more than 100 point releases (ie any number is > 100) and also will declare 1.5.0 == 1.5. That said, its short, sweet, and simple to use.

EDIT: if you use the NSString 'compare:options:' method, make sure you have your string well groomed:

    s1 = @"1.";
    s2 = @"1";
    NSLog(@"Compare %@ to %@ result %d", s1, s2, (int)[s1 compare:s2 options:NSNumericSearch]);
    s1 = @"20.20.0";
    s2 = @"20.20";
    NSLog(@"Compare %@ to %@ result %d", s1, s2, (int)[s1 compare:s2 options:NSNumericSearch]);

2012-09-06 11:26:24.793 xxx[59804:f803] Compare 1. to 1 result 1
2012-09-06 11:26:24.794 xxx[59804:f803] Compare 20.20.0 to 20.20 result 1
David H
  • 40,852
  • 12
  • 92
  • 138
  • Hmmm, this gives me more to consider, not sure how to make this into a workable solution. – Jules Sep 06 '12 at 15:56
  • I wouldn't waste much time on it. My point was that the compare solution is shorter but is not a true "version number" compare function. There were C functions in the "duplicate" link. You can probably find a huge number of possible solutions. I probably would have used the compare:options if I'd known about it when I coded mine. – David H Sep 06 '12 at 16:00