Skip to content

Instantly share code, notes, and snippets.

@hanfengs
Created July 12, 2019 01:57
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save hanfengs/e3eaf76d77092a1f05b40a679ad002d6 to your computer and use it in GitHub Desktop.
[版本号比较] 版本号比较oc实现
/**
@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