Obj-C,如何处理4个版本的版本号,大于等等

时间:2022-07-02 06:56:16

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.

我知道这不是一项具有挑战性的练习,但我想我会问,以防万一已经有一些方法已经在Objective-c等。

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

在我的应用程序中,我只处理2号版本号,例如1.5

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

我想将它升级为4个数字,最多可以有4个数字。

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

因此,我需要处理现有数字,并在传递数据库版本和包版本号时返回false。

At the moment I simply do

目前我只是这样做

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

}

2 个解决方案

#1


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;

#2


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.

这样做可以为您提供可用于比较的标准化值,并且通常会比较具有不同“深度”的版本,即1.5和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.

如果你有超过100个点发布(即任何数字> 100),它会中断,并且还会声明1.5.0 == 1.5。也就是说,它简短,甜美,易于使用。

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

编辑:如果你使用NSString'compare:options:'方法,请确保你的字符串整齐:

    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

#1


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;

#2


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.

这样做可以为您提供可用于比较的标准化值,并且通常会比较具有不同“深度”的版本,即1.5和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.

如果你有超过100个点发布(即任何数字> 100),它会中断,并且还会声明1.5.0 == 1.5。也就是说,它简短,甜美,易于使用。

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

编辑:如果你使用NSString'compare:options:'方法,请确保你的字符串整齐:

    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