1

I'm trying to compare a software version... let's say 5.4.9

companysettings.software is a nsstring

if ([companysettings.software version //some type of value] < 5.4.9) {

//you need to update blah blah blah

} else {

//proceed with app flow

}

How do I compare a number that has more than one decimal?

Hackmodford
  • 3,901
  • 4
  • 35
  • 78

6 Answers6

3

IMO it's better to separate the components and compare them consecutively. Making the version number a single number will introduce a possibility for error (e.g. 5.1.2 = 5.12).

Alexander
  • 8,117
  • 1
  • 35
  • 46
  • that makes sense... how would I split the string then? – Hackmodford Jun 28 '12 at 13:24
  • [`componentsSeparatedByString:`](https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html) should be adequate. – Alexander Jun 28 '12 at 13:25
1

This is the better answer... In this case the version I'm looking for is 6.0.6 6.0.5 will trigger the versionLower to TRUE

NSArray *versionStrings = [companySettings.softwareVersion componentsSeparatedByString:@"."];

BOOL versionLower = FALSE;

for (int i = 0; i < 3; i++) {

    switch (i) {
        case 0:

            if ([[versionStrings objectAtIndex:0] intValue] < 6) {
                versionLower = TRUE;
            }
            break;

        case 1:

            if ([[versionStrings objectAtIndex:1] intValue] < 0) {
                versionLower = TRUE;
            }
            break;

        case 2:

            if ([[versionStrings objectAtIndex:2] intValue] < 6) {
                versionLower = TRUE;
            }
            break;

        default:
            break;
    } 
}

if (!versionLower) {

//version of software is okay... proceed with flow

}
Hackmodford
  • 3,901
  • 4
  • 35
  • 78
1

This post has had the best answer I've found yet Compare version numbers in Objective-C

This is the simplest way to compare versions, keeping in mind that "1" < "1.0" < "1.0.0":

NSString* requiredVersion = @"1.2.0";
NSString* actualVersion = @"1.1.5";

if ([requiredVersion compare:actualVersion options:NSNumericSearch] == NSOrderedDescending) {
  // actualVersion is lower than the requiredVersion
}
Community
  • 1
  • 1
Hackmodford
  • 3,901
  • 4
  • 35
  • 78
0
//Suppose these are the versions
NSString *version1 = @"4.3.2";
NSString *version2 = @"3.2.1.8";

//split first two characters of your version
NSString *first2digitversion1 = [version1 substringToIndex:2];
NSString *first2digitversion2 = [version2 substringToIndex:2];

//now get remaining part of string
NSString *remainVersion1 = [version1 substringFromIndex:2];
NSString *remainVersion2 = [version2 substringFromIndex:2];

//now remove the "." from string
remainVersion1 = [remainVersion1 stringByReplacingOccurrencesOfString:@"." withString:@""];
remainVersion2 = [remainVersion2 stringByReplacingOccurrencesOfString:@"." withString:@""];

//now merge both
NSString *comPareStr1 = [[NSString stringWithFormat:first2digitversion1]stringByAppendingString:remainVersion1];
NSString *comPareStr2 = [[NSString stringWithFormat:first2digitversion2]stringByAppendingString:remainVersion2];

// now you will get 4.32 and 3.218 
//and this is the comparision
if([comPareStr1 floatValue] > [comPareStr2 floatValue])
{
    NSLog(@"comPareStr1 is greater");
}
else
{
    NSLog(@"comPareStr2 id greater");
}
TheTiger
  • 13,264
  • 3
  • 57
  • 82
0

Do some modification according to your requirment and prevent to arrayIndexOfBoundException : but the logic would be easy to understand ..

 NSString *firstVersion = @"5.4.9";
 NSArray *firstChunks = [firstVersion componentsSeparatedByString: @"."];

 NSString *secondVersion = @"6.4.9";
 NSArray *secondChunks = [secondVersion componentsSeparatedByString: @"."];

  for(int i = 0 ; i< [firstChunks count] ; i++){
      int first = [[firstChunks objectAtIndex:i] intValue];
      int second = [[secondChunks objectAtIndex:i] intValue];
      if(first>second){
         NSLog(@"first version in heigher");
     }
     else
         NSLog(@"second version in heigher");
}

may this will help you

Abhishek
  • 2,255
  • 1
  • 13
  • 21
  • This will not produce a response if the first version is 5.4.9 and the second is 5.4.9.2 or if the versions are equal. – Alexander Jun 28 '12 at 14:02
  • yes that's why i have told you make the thing at your requirement ... you can put the condition before it like which array count would be greater then do according to that... got my point.. i just provided you the basic idea how you can do it easily .. – Abhishek Jun 28 '12 at 14:06
  • you can put your condition before for just before it and it will work fine – Abhishek Jun 28 '12 at 14:07
-1

This is what I did...

strip the decimals out of the string then compare

NSString *stringWithoutDecimals = [companySettings.softwareVersion stringByReplacingOccurrencesOfString:@"." withString:@""];

if ([stringWithoutDecimals intValue]  >= 606) {

}
Hackmodford
  • 3,901
  • 4
  • 35
  • 78