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,338
  • 14
  • 96
  • 181

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,251
  • 1
  • 19
  • 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,082
  • 12
  • 89
  • 132
  • 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