Created
September 21, 2013 04:24
-
-
Save Hardtack/6647210 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// HTSemanticVersion.h | |
// | |
// Created by 최건우 on 13. 7. 25.. | |
// Copyright (c) 2013년 Hardtack. All rights reserved. | |
// | |
#import "HTVersion.h" | |
@interface HTSemanticVersion : HTVersion | |
@property (nonatomic) NSInteger majorVersion; | |
@property (nonatomic) NSInteger minorVersion; | |
@property (nonatomic) NSInteger patchVersion; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// HTSemanticVersion.m | |
// | |
// Created by 최건우 on 13. 7. 25.. | |
// Copyright (c) 2013년 Hardtack. All rights reserved. | |
// | |
#import "HTSemanticVersion.h" | |
@implementation HTSemanticVersion | |
#pragma mark - Initializers | |
- (id)initWithString:(NSString *)string { | |
self = [super initWithString:string]; | |
if (self) { | |
NSArray *components = [string componentsSeparatedByString:@"."]; | |
if ([components count] > 3) { // Invalid string | |
return nil; | |
} | |
NSInteger versions[3] = {0,}; | |
NSUInteger i = 0; | |
for (NSString *component in components) { | |
NSScanner *scanner = [NSScanner scannerWithString:component]; | |
NSInteger v; | |
if (![scanner scanInteger:&v]) { | |
return nil; | |
} | |
versions[i] = v; | |
i++; | |
} | |
self.majorVersion = versions[0]; | |
self.minorVersion = versions[1]; | |
self.patchVersion = versions[2]; | |
} | |
return self; | |
} | |
- (NSString *)description { | |
return [NSString stringWithFormat:@"%d.%d.%d", self.majorVersion, self.minorVersion, self.patchVersion]; | |
} | |
- (NSComparisonResult)compare:(HTVersion *)version { | |
if (![version isKindOfClass:[self class]]) { | |
return [super compare:version]; | |
} | |
HTSemanticVersion *v = (HTSemanticVersion *)version; | |
NSComparisonResult r = NSIntegerCompare(self.majorVersion, v.majorVersion); | |
if (r == NSOrderedSame) { | |
r = NSIntegerCompare(self.minorVersion, v.minorVersion); | |
} | |
if (r == NSOrderedSame) { | |
r = NSIntegerCompare(self.patchVersion, v.patchVersion); | |
} | |
return r; | |
} | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// HTVersion.h | |
// | |
// Created by 최건우 on 13. 7. 25.. | |
// Copyright (c) 2013년 Hardtack. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
@interface HTVersion : NSObject | |
@property (strong, nonatomic) NSString *string; | |
+ (instancetype)versionWithNumber:(NSNumber *)number; | |
+ (instancetype)versionWithString:(NSString *)string; | |
- (id)initWithString:(NSString *)string; | |
// Default implementation is not reliable. You must override it. | |
- (NSComparisonResult)compare:(HTVersion *)version; | |
@end | |
NSComparisonResult NSUIntegerCompare(NSUInteger left, NSUInteger right); | |
NSComparisonResult NSIntegerCompare(NSInteger left, NSInteger right); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// HTVersion.m | |
// | |
// Created by 최건우 on 13. 7. 25.. | |
// Copyright (c) 2013년 Hardtack. All rights reserved. | |
// | |
#import "HTVersion.h" | |
@implementation HTVersion | |
#pragma mark - Private methods | |
#pragma mark - Class methods | |
+ (instancetype)versionWithString:(NSString *)string { | |
return [[[self class] alloc] initWithString:string]; | |
} | |
+ (instancetype)versionWithNumber:(NSNumber *)number { | |
return [self versionWithString:[number description]]; | |
} | |
#pragma mark - Initializers | |
- (id)init { | |
return [self initWithString:nil]; | |
} | |
- (id)initWithString:(NSString *)string { | |
self = [super init]; | |
if (self) { | |
self.string = string; | |
} | |
return self; | |
} | |
#pragma mark - Public methods | |
- (NSComparisonResult)compare:(HTVersion *)version { | |
return [self.string isEqualToString:version.string] ? NSOrderedSame : NSOrderedDescending; | |
} | |
@end | |
NSComparisonResult NSUIntegerCompare(NSUInteger left, NSUInteger right) { | |
if (left < right) { | |
return NSOrderedAscending; | |
} else if (left > right) { | |
return NSOrderedDescending; | |
} else { | |
return NSOrderedSame; | |
} | |
} | |
NSComparisonResult NSIntegerCompare(NSInteger left, NSInteger right) { | |
if (left < right) { | |
return NSOrderedAscending; | |
} else if (left > right) { | |
return NSOrderedDescending; | |
} else { | |
return NSOrderedSame; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int main(int argc, char *argv[]) { | |
HTVersion *systemVersion = [[HTSemanticVersion alloc] initWithString:[[UIDevice currentDevice] systemVersion]]; | |
if ([systemVersion compare:[[HTSemanticVersion alloc] initWithString:@"7.0"]] == NSOrderedDescending) { | |
... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment