2020-06-30突然想到一個比較騷的比較辦法盆赤,去掉版本號中間的 "." 然后比大小贾富。
字符串前拼接 "0.",是為了方便后續(xù)拓展版本(例如:1.0升級到1.0.1)
- (BOOL)versionCompareFirst:(NSString *)first andVersionSecond:(NSString *)second
{
first = [first stringByReplacingOccurrencesOfString:@"." withString:@""];
first = [@"0." stringByAppendingString:first];
second = [second stringByReplacingOccurrencesOfString:@"." withString:@""];
second = [@"0."stringByAppendingString:second];
return [first floatValue] >= [second floatValue];
}
===========================2020-10-13 更新(果然憑空想象的都不靠譜)
遺忘了1.9到1.10的這類情況牺六,還是老老實實遍歷吧
- (BOOL)versionCompareFirst:(NSString *)first andVersionSecond:(NSString *)second
{
NSArray *firstArray = [first componentsSeparatedByString:@"."];
NSArray *secondArray = [second componentsSeparatedByString:@"."];
for (int i = 0; i < firstArray.count && i < secondArray.count; i++) {
NSInteger a = [[firstArray objectAtIndex:i] integerValue];
NSInteger b = [[secondArray objectAtIndex:i] integerValue];
if (a > b) {
return YES;
}
}
return NO;
}