Created
October 23, 2017 13:30
-
-
Save dodikk/ba94d868f3a93bf87f462ac9847664c0 to your computer and use it in GitHub Desktop.
Time based UUID generator - [obj-c] [Objective-C]
This file contains 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
#import <Foundation/Foundation.h> | |
@interface TimeBasedUuidGenerator : NSObject | |
-(NSString*)generateTimeBasedUuid; | |
-(NSUUID*)generateTimeBasedUuidObject; | |
@end | |
This file contains 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
#import "TimeBasedUuidGenerator.h" | |
#import <uuid/uuid.h> | |
@implementation TimeBasedUuidGenerator | |
-(NSString*)generateTimeBasedUuid | |
{ | |
uuid_t result; | |
uuid_generate_time(result); | |
// https://gist.github.com/yoggy/4483031 | |
// ex. "1b4e28ba-2fa1-11d2-883f-0016d3cca427" + "\0" | |
char uuid_str[37] = {0}; | |
uuid_unparse_upper(result, uuid_str); | |
NSString* wrappedResult = [NSString stringWithUTF8String: uuid_str]; | |
return wrappedResult; | |
} | |
-(NSUUID*)generateTimeBasedUuidObject | |
{ | |
NSString* strUuid = [self generateTimeBasedUuid]; | |
NSUUID* result = [[NSUUID alloc] initWithUUIDString: strUuid]; | |
return result; | |
} | |
@end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment