-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBPUUID.m
More file actions
115 lines (85 loc) · 2.13 KB
/
BPUUID.m
File metadata and controls
115 lines (85 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//
// BPUUID.m
// Skates
//
// Created by Jon Olson on 2/3/10.
// Copyright 2010 Ballistic Pigeon, LLC. All rights reserved.
//
#import "BPUUID.h"
@interface BPUUID (Private)
@end
@implementation BPUUID
#pragma mark -
#pragma mark Construction and deallocation
+ (BPUUID *)UUID {
return [[[self alloc] init] autorelease];
}
+ (BPUUID *)UUIDWithCFUUID:(CFUUIDRef)uuid {
return [[[self alloc] initWithCFUUID:uuid] autorelease];
}
+ (BPUUID *)UUIDWithString:(NSString *)uuidString {
return [[[self alloc] initWithString:uuidString] autorelease];
}
+ (BPUUID *)UUIDWithBytes:(NSData *)uuidBytes {
return [[[self alloc] initWithBytes:uuidBytes] autorelease];
}
- (id)init {
if (self = [super init]) {
CFUUID = CFUUIDCreate(NULL);
}
return self;
}
- (id)initWithCFUUID:(CFUUIDRef)uuid {
if (self = [super init]) {
CFRetain(uuid);
CFUUID = uuid;
}
return self;
}
- (id)initWithString:(NSString *)uuidString {
if (self = [super init]) {
CFUUID = CFUUIDCreateFromString(NULL, (CFStringRef)uuidString);
}
return self;
}
- (id)initWithBytes:(NSData *)uuidBytes {
if (self = [super init]) {
CFUUIDBytes bytes;
assert(sizeof(bytes) == [uuidBytes length]);
[uuidBytes getBytes:&bytes];
CFUUID = CFUUIDCreateFromUUIDBytes(NULL, bytes);
}
return self;
}
- (void)dealloc {
CFRelease(CFUUID);
[super dealloc];
}
#pragma mark -
#pragma mark Accessors
@synthesize CFUUID;
#pragma mark -
#pragma mark UUID Represnetations
- (NSString *)stringRepresentation {
NSString *stringRep = (NSString *)CFUUIDCreateString(NULL, CFUUID);
return [stringRep autorelease];
}
- (NSData *)byteRepresentation {
CFUUIDBytes bytes = CFUUIDGetUUIDBytes(CFUUID);
return [NSData dataWithBytes:&bytes length:sizeof(bytes)];
}
#pragma mark -
#pragma mark Description
- (NSString *)description {
return [self stringRepresentation];
}
#pragma mark -
#pragma mark NSCoding implementation
- (id)initWithCoder:(NSCoder *)decoder {
NSString *uuidString = [decoder decodeObjectForKey:@"UUID"];
return [self initWithString:uuidString];
}
- (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:[self stringRepresentation] forKey:@"UUID"];
}
@end