Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. /**
  2. @return 如果版本号相等返回0,第一个版本号高于第二个,返回1,否则返回 -1.
  3. */
  4. - (NSInteger)compareVersion:(NSString *)version1 andVersion:(NSString *)version2{
  5. NSInteger res = 0;
  6. NSArray *arry1 = [version1 componentsSeparatedByString:@"."];
  7. NSArray *arry2 = [version2 componentsSeparatedByString:@"."];
  8.  
  9. NSInteger idx1 = 0;
  10. NSInteger idx2 = 0;
  11.  
  12. while (res == 0 && (idx1 < arry1.count || idx2 < arry2.count)) {
  13.  
  14. id num1 = idx1 < arry1.count ? arry1[idx1] : 0;
  15. id num2 = idx2 < arry2.count ? arry2[idx2] : 0;
  16.  
  17. if ([num1 integerValue] > [num2 integerValue]) {
  18. res = 1;
  19. }else if ([num1 integerValue] < [num2 integerValue]) {
  20. res = -1;
  21. }
  22.  
  23. idx1++;
  24. idx2++;
  25. }
  26. return res;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement