Skip to content

Instantly share code, notes, and snippets.

@dodikk
Created October 23, 2017 13:30
Show Gist options
  • Save dodikk/ba94d868f3a93bf87f462ac9847664c0 to your computer and use it in GitHub Desktop.
Save dodikk/ba94d868f3a93bf87f462ac9847664c0 to your computer and use it in GitHub Desktop.
Time based UUID generator - [obj-c] [Objective-C]
#import <Foundation/Foundation.h>
@interface TimeBasedUuidGenerator : NSObject
-(NSString*)generateTimeBasedUuid;
-(NSUUID*)generateTimeBasedUuidObject;
@end
#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