1

我正在尝试比较软件版本...比如说 5.4.9

companysettings.software 是一个 nsstring

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

//you need to update blah blah blah

} else {

//proceed with app flow

}

如何比较多于一位小数的数字?

4

6 回答 6

3

IMO最好将组件分开并连续比较它们。将版本号设为单个数字将引入错误的可能性(例如 5.1.2 = 5.12)。

于 2012-06-28T13:22:34.107 回答
1

这是更好的答案......在这种情况下,我正在寻找的版本是 6.0.6 6.0.5 将触发 versionLower 为 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

}
于 2012-06-28T13:37:04.660 回答
1

这篇文章有我找到的最佳答案比较 Objective-C 中的版本号

这是比较版本的最简单方法,请记住 "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
}
于 2012-07-03T13:09:18.013 回答
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");
}
于 2012-06-28T13:45:02.363 回答
0

根据您的要求进行一些修改并防止 arrayIndexOfBoundException :但逻辑很容易理解..

 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");
}

这可能会帮助你

于 2012-06-28T13:57:17.500 回答
-1

这就是我所做的...

从字符串中去掉小数,然后比较

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

if ([stringWithoutDecimals intValue]  >= 606) {

}
于 2012-06-28T13:21:11.507 回答