OK, I’m not seriously looking to use some sort of exit command to quit the loop, but in this situation, I tend to use two methods, when I’m sure I could use one.
With this example, what is the best approach ?
typedef enum {
FirstParamHigherThanSecond,
FirstParamLowerThanSecond,
ParamsEqual
} VersionStatus;
VersionStatus compareVersions(NSString* left, NSString* right) {
VersionStatus retVal;
NSComparisonResult res = analyszeVersions(left, right);
if (res == NSOrderedSame) {
retVal = ParamsEqual;
} else if (res == NSOrderedDescending) {
retVal = FirstParamHigherThanSecond;
} else if ( res == NSOrderedAscending) {
retVal = FirstParamLowerThanSecond;
}
return retVal;
}
.
NSComparisonResult analyszeVersions(NSString* leftVersion, NSString* rightVersion)
{
int i;
NSMutableArray *leftFields = [[NSMutableArray alloc] initWithArray:[leftVersion componentsSeparatedByString:@"."]];
NSMutableArray *rightFields = [[NSMutableArray alloc] initWithArray:[rightVersion componentsSeparatedByString:@"."]];
if ([leftFields count] < [rightFields count]) {
while ([leftFields count] != [rightFields count]) {
[leftFields addObject:@"0"];
}
} else if ([leftFields count] > [rightFields count]) {
while ([leftFields count] != [rightFields count]) {
[rightFields addObject:@"0"];
}
}
for(i = 0; i < [leftFields count]; i++) {
NSComparisonResult result = [[leftFields objectAtIndex:i] compare:[rightFields objectAtIndex:i] options:NSNumericSearch];
if (result != NSOrderedSame) {
return result;
}
}
return NSOrderedSame;
}
If I understand your question correctly, you could just use a
whileloop instead of yourforloop, like: