Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如果Objective-C中的一个版本号中包含较少的部分,如何对版本号进行比较?_Iphone_Objective C_Ios - Fatal编程技术网

Iphone 如果Objective-C中的一个版本号中包含较少的部分,如何对版本号进行比较?

Iphone 如果Objective-C中的一个版本号中包含较少的部分,如何对版本号进行比较?,iphone,objective-c,ios,Iphone,Objective C,Ios,我在上找到了以下代码 这很好,几乎正是我想要的,但是如果我使用值@“1.4.5”,@“10.4”,它会产生错误的结果,说第一个数字更低 arghhh深夜编码,对不起,我把10.4理解为1.4:( 我不确定compare为什么会有问题,问题是什么 /* * compareVersions(@"10.4", @"10.3"); // returns NSOrderedDescending (1) - aka first number

我在上找到了以下代码

这很好,几乎正是我想要的,但是如果我使用值
@“1.4.5”,@“10.4”
,它会产生错误的结果,说第一个数字更低

arghhh深夜编码,对不起,我把10.4理解为1.4:(

我不确定compare为什么会有问题,问题是什么

/*
 * compareVersions(@"10.4",             @"10.3"); //             
       returns NSOrderedDescending (1) - aka first number is higher

 * compareVersions(@"10.5",             @"10.5.0"); //           
       returns NSOrderedSame (0) 

 * compareVersions(@"10.4 Build 8L127", @"10.4 Build 8P135"); // 
       returns NSOrderedAscending (-1) - aka first number is lower
 */
NSComparisonResult compareVersions(NSString* leftVersion, NSString* rightVersion)
{
    int i;

    // Break version into fields (separated by '.')
    NSMutableArray *leftFields  = [[NSMutableArray alloc] initWithArray:[leftVersion  componentsSeparatedByString:@"."]];
    NSMutableArray *rightFields = [[NSMutableArray alloc] initWithArray:[rightVersion componentsSeparatedByString:@"."]];

    // Implict ".0" in case version doesn't have the same number of '.'
    if ([leftFields count] < [rightFields count]) {
        while ([leftFields count] != [rightFields count]) {
            [leftFields addObject:@"0"];
        }
    } else if ([leftFields count] > [rightFields count]) {
        while ([leftFields count] != [rightFields count]) {
            [rightFields addObject:@"0"];
        }
    }
/*
*比较(@“10.4”和@“10.3”);//
返回取消搜索(1)-aka第一个数字更高
*比较(@“10.5”,@“10.5.0”);//
返回DeredName(0)
*比较(@“10.4版本8L127”和@“10.4版本8P135”);//
返回取消排序(-1)-aka第一个数字较低
*/
NSComparisonResult比较(NSString*leftVersion,NSString*rightVersion)
{
int i;
//将版本拆分为多个字段(以“.”分隔)
NSMutableArray*leftFields=[[NSMutableArray alloc]initWithArray:[leftVersion componentsSeparatedByString:@.”]];
NSMutableArray*rightFields=[[NSMutableArray alloc]initWithArray:[rightVersion componentsSeparatedByString:@.”]];
//隐式“.0”,以防版本的“.”数不相同
如果([leftFields count]<[rightFields count]){
而([leftFields count]!=[rightFields count]){
[leftFields addObject:@“0”];
}
}如果([leftFields count]>[rightFields count]){
而([leftFields count]!=[rightFields count]){
[rightFields addObject:@“0”];
}
}

//对每个字段进行数字比较
对于(i=0;i<[leftFields count];i++){
NSComparisonResult=[[leftFields objectAtIndex:i]比较:[rightFields objectAtIndex:i]选项:NSNumericSearch];
如果(结果!=SensorDeredName){
[左字段释放];
[rightFields发布];
返回结果;
}
}
[左字段释放];
[rightFields发布];
返回指定名称;
}

[我今天早些时候发布了这篇文章,但没有选择它作为答案,它可能更适合您的问题。还有其他技术,您可以寻找其他解决方案。]

我要做的是将该字符串拆分为多个组件:

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

NSInteger value = 0;
NSInteger multiplier = 1000000;
for(NSString *n in array) {
  value += [n integerValue] * multiplier;
  multiplier /= 100;
}
这样做的目的是为您提供一个可用于比较的标准化值,并且通常会比较具有不同“深度”的版本,即1.5和1.5.2

如果您有超过100个点版本(即任何数字大于100),它将中断,并且还将声明1.5.0==1.5。也就是说,它很短,很好,而且使用简单

编辑:如果使用NSString“比较:选项:”方法,请确保字符串经过精心整理:

    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

Sparkle Mac框架是开源的,它有一些简洁的版本检查代码,你可以看一下:

所以你想把10.5和1.4.6进行比较,这样10.5就被认为是0.10.5

如果是这种情况,则需要将“0”数组项添加到单独版本号的左侧

NSComparisonResult compareVersions(NSString* leftVersion, NSString* rightVersion)
{
    int i;

    // Break version into fields (separated by '.')
    NSMutableArray *leftFields  = [[NSMutableArray alloc] initWithArray:[leftVersion  componentsSeparatedByString:@"."]];
    NSMutableArray *rightFields = [[NSMutableArray alloc] initWithArray:[rightVersion componentsSeparatedByString:@"."]];

    // Implict "0" in case version doesn't have the same number of '.'
    if ([leftFields count] < [rightFields count]) {
        while ([leftFields count] != [rightFields count]) {
            [leftFields insertObject:@"0" atIndex:0];
        }
    } else if ([leftFields count] > [rightFields count]) {
        while ([leftFields count] != [rightFields count]) {
            [rightFields insertObject:@"0" atIndex:0];
        }
    }
NSComparisonResult比较(NSString*leftVersion,NSString*rightVersion)
{
int i;
//将版本拆分为多个字段(以“.”分隔)
NSMutableArray*leftFields=[[NSMutableArray alloc]initWithArray:[leftVersion componentsSeparatedByString:@.”]];
NSMutableArray*rightFields=[[NSMutableArray alloc]initWithArray:[rightVersion componentsSeparatedByString:@.”]];
//如果版本的“.”数不相同,则隐式“0”
如果([leftFields count]<[rightFields count]){
而([leftFields count]!=[rightFields count]){
[左字段插入对象:@“0”索引:0];
}
}如果([leftFields count]>[rightFields count]){
而([leftFields count]!=[rightFields count]){
[右字段插入对象:@“0”索引:0];
}
}

我不能100%确定你在问什么,但如果你想对数字进行排序,而不管有多少个句点,“.”在数字中,你可以在操作系统版本字典上使用
NSSortDescriptor

NSArray *originalArray = [NSArray arrayWithObjects:
              [NSDictionary dictionaryWithObject:@"10.4.5" forKey:@"version"],
              [NSDictionary dictionaryWithObject:@"10.5.6" forKey:@"version"],
              [NSDictionary dictionaryWithObject:@"10.6.8" forKey:@"version"],
              [NSDictionary dictionaryWithObject:@"10.8" forKey:@"version"],
              [NSDictionary dictionaryWithObject:@"10.7.1" forKey:@"version"],
              [NSDictionary dictionaryWithObject:@"10.8.2" forKey:@"version"],
              nil];

NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"version" ascending:true];
NSArray *sortedArray = [originalArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];
NSLog(@"Lowest to highest: %@", sortedArray);
NSLog(@"Highest OS version: %@",[[sortedArray objectAtIndex:[sortedArray indexOfObject:[sortedArray lastObject]]] objectForKey:@"version"]);

我的感觉是,第一个数字组总是最重要的,所以10.任何东西都比9.任何东西大。如果我说得对,那么解决方案就是用零替换点,并用零填充右手边的短字符串,以匹配长字符串的长度:

e.g.
9.4   --->  90400  (padded on the right with 00)
8.6.7 --->  80607
这样做的好处是,如果我对需求的理解有误,那么通过在右边填充较短的字符串,可以很容易地修复算法

- (NSComparisonResult)compareVersion:(NSString *)vA withVersion:(NSString *)vB {

    NSString *vAPadded = [vA stringByReplacingOccurrencesOfString:@"." withString:@"0"];
    NSString *vBPadded = [vB stringByReplacingOccurrencesOfString:@"." withString:@"0"];

    while (vAPadded.length < vBPadded.length)
        vAPadded = [vAPadded stringByAppendingString:@"0"];

    while (vBPadded.length < vAPadded.length)
        vBPadded = [vBPadded stringByAppendingString:@"0"];

    return [vAPadded intValue] - [vBPadded intValue];
}

为什么不使用NSString比较:选项:NSNumericSearch

NSString *sysVer = [[UIDevice currentDevice] systemVersion];
NSLog(@"%@,%d,%d,%d", sysVer, [sysVer compare:@"1.0" options: NSNumericSearch], [sysVer compare:@"6.0" options: NSNumericSearch],[sysVer compare:@"10.0" options: NSNumericSearch]);
if ([sysVer compare:@"6.0" options: NSNumericSearch]>=NSOrderedSame) {
    NSLog(@"ios 6");
}

您可以使用my Version类帮助您将版本字符串解析为版本对象,以便于比较。它支持4个字段版本号,如major.minor.release.build,所有字段都是可选的。此外,它还有一个比较方法,用于比较两个版本对象


版本比较类很简单

BOOL更大=[VersionComparator isVersion:@“2.0.0”比Version:@“1.1.0”更大];

正如在这篇文章中回答的那样

查看我的NSString类别,它在github上实现了简单的版本检查

[@“1.2.2.4”比较版本:@“1.2.2.5”;
这将返回一个NSComparisonResult,该结果比使用更准确

[@“1.2.2”比较:@“1.2.2.5”选项:NSNumericSearch]
还添加了助手

[@“1.2.2.4”isOlderThanVersion:@“1.2.2.5”;
[@“1.2.2.4”是新版本:@“1.2.2.5”;
[@“1.2.2.4”等高版本:@“1.2.2.5”];
[@“1.2.2.4”与“1.2.2.5”版本相同;
[@“1.2.2.4”等同于新版本:@“1.2.2.5”;

为什么您希望10.4低于1.4.5?这通常不是版本号的工作方式…对不起,可能是个愚蠢的问题,但您想应用什么测试?是什么让1.4.5大于10.4?第
vAPadded = [@"0" stringByAppendingString:vAPadded];
NSString *sysVer = [[UIDevice currentDevice] systemVersion];
NSLog(@"%@,%d,%d,%d", sysVer, [sysVer compare:@"1.0" options: NSNumericSearch], [sysVer compare:@"6.0" options: NSNumericSearch],[sysVer compare:@"10.0" options: NSNumericSearch]);
if ([sysVer compare:@"6.0" options: NSNumericSearch]>=NSOrderedSame) {
    NSLog(@"ios 6");
}