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?
I don’t know of anything built in that will do it, but I have heard that the Sparkle framework has a version comparator.
Browsing quickly through the source reveals that the SUStandardVersionComparator object seems to be in charge of it. It conforms to the
<SUVersionComparison>protocol,which means you could probably just use it like this:(note: code untested and typed in a browser. Caveat Implementor)