Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing version numbers

Some time ago, I read that comparing version numbers can be done using the following code snippet:

NSString *vesrion_1 = @"1.2.1";
NSString *version_2 = @"1.2.0";

if ([version_1 compare:version_2 options:NSNumericSearch] == NSOrderedAscending) {
...
}

This seems to work fine, but one user is having a problem, which is due to a seemingly incorrect version number comparison.

My question is, is it safe under all circumstances to use this technique to compare version numbers? Is it possible that the above comparison results in different outcomes on different machines?

like image 418
stacktrace Avatar asked Jun 13 '26 14:06

stacktrace


1 Answers

I've developed a VersionComparator on GitHub that is very light and simple to use - not as feature packed as some other solutions but easy to pick up and use.

BOOL greater = [VersionComparator isVersion:@"2.0.0" greaterThanVersion:@"1.1.0"];

It simply compares the numbers from the major to the build - if 2 is higher than 1 then there's no need to compare further.

It's aim is to provide the front-end code as simple as possible (as in the example above), and also not provide a class that has reams and reams of supporting code. More often than not, this is all that's needed.

like image 105
Dan Hanly Avatar answered Jun 17 '26 21:06

Dan Hanly



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!