Created
July 12, 2019 01:57
Star
You must be signed in to star a gist
[版本号比较] 版本号比较oc实现
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
@return 如果版本号相等返回0,第一个版本号高于第二个,返回1,否则返回 -1. | |
*/ | |
- (NSInteger)compareVersion:(NSString *)version1 andVersion:(NSString *)version2{ | |
NSInteger res = 0; | |
NSArray *arry1 = [version1 componentsSeparatedByString:@"."]; | |
NSArray *arry2 = [version2 componentsSeparatedByString:@"."]; | |
NSInteger idx1 = 0; | |
NSInteger idx2 = 0; | |
while (res == 0 && (idx1 < arry1.count || idx2 < arry2.count)) { | |
id num1 = idx1 < arry1.count ? arry1[idx1] : 0; | |
id num2 = idx2 < arry2.count ? arry2[idx2] : 0; | |
if ([num1 integerValue] > [num2 integerValue]) { | |
res = 1; | |
}else if ([num1 integerValue] < [num2 integerValue]) { | |
res = -1; | |
} | |
idx1++; | |
idx2++; | |
} | |
return res; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment