There are about a million ways you could go about this, but here is a quick example:
void versionStringComponents(NSString* versionStr_, NSInteger* major__, NSInteger* minor__, NSInteger* bugfix__)
{
NSArray* elements = [versionStr_ componentsSeparatedByString:@"."];
*major__ = [[elements objectAtIndex:0] intValue];
*minor__ = [[elements objectAtIndex:1] intValue];
*bugfix__ = 0;
if([elements count] > 2)
{
*bugfix__ = [[elements objectAtIndex:2] intValue];
}
}
bool versionLessThan(NSString* versionStr1_, NSString* versionStr2_)
{
NSInteger major1 = 0, minor1 = 0, bugfix1 = 0;
versionStringComponents(versionStr1_, &major1, &minor1, &bugfix1);
NSInteger major2 = 0, minor2 = 0, bugfix2 = 0;
versionStringComponents(versionStr2_, &major2, &minor2, &bugfix2);
return (
major1 < major2 ||
(major1 == major2 && minor1 < minor2) ||
(major1 == major2 && minor1 == minor2 && bugfix1 < bugfix2)
);
}
Obviously this is a quick little hack, as versionStringComponents()
just blindly separates the version numbers from a string, turns them into ints, etc. Also, it could use a few more comparison functions, but I'm sure you can figure those out.
This link, as mentioned by Saphrosit, is a much shorter way of accomplishing the same task, but requires the use of code from the Sparkle framework.