Skip to content

Instantly share code, notes, and snippets.

@hanfengs hanfengs/compare.m
Created Jul 12, 2019

Embed
What would you like to do?
[版本号比较] 版本号比较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
You can’t perform that action at this time.