Skip to content

Objective-C Version String Comparison

Often in code you have to compare two version strings, such as 1.0.0.1 and 1.2. You can not just convert these to fractions since there are multiple periods which do not correlate to a decimal number. The best way I found was to use the build in string comparator with some options.

Listed below is a category I extend NSString with to do version string comparisons. Code can be downloaded here: NSString+Version


@implementation NSString(Version)

-(BOOL) isOlderVersionThan:(NSString*)otherVersion
{
return ([self compare:otherVersion options:NSNumericSearch] == NSOrderedAscending);
}

-(BOOL) isNewerVersionThan:(NSString*)otherVersion
{
return ([self compare:otherVersion options:NSNumericSearch] == NSOrderedDescending);
}

@end

Post a Comment

Your email is never published nor shared. Required fields are marked *