如何使用苹果自带的UUID

TL;DR

其实苹果以 C 函数的方式提供了 UUID 的方法

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
- (void)testUUID
{
uuid_t u;
uuid_t ur;
uuid_t ut;

uuid_generate(u);
uuid_generate_random(ur);
uuid_generate_time(ut);

char a[16];
uuid_unparse(u, a);
char b[16];
uuid_unparse(ur, b);
char c[16];
uuid_unparse(ut, c);

// printf("%s\n", a);
// printf("%s\n", b);
printf("%s\n", c);
}

- (NSString *)uuid{
// Create universally unique identifier (object)
CFUUIDRef uuidObject = CFUUIDCreate(kCFAllocatorDefault);

// Get the string representation of CFUUID object.
NSString *uuidStr = (NSString *)CFBridgingRelease(CFUUIDCreateString(kCFAllocatorDefault, uuidObject));

// If needed, here is how to get a representation in bytes, returned as a structure
// typedef struct {
// UInt8 byte0;
// UInt8 byte1;
// ...
// UInt8 byte15;
// } CFUUIDBytes;
// CFUUIDBytes bytes = CFUUIDGetUUIDBytes(uuidObject);

CFRelease(uuidObject);

return uuidStr;
}

测试代码:

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
//
// ViewController.m
// test
//
// Created by Ashbringer on 7/7/15.
// Copyright (c) 2015 Dian.fm. All rights reserved.
//

#import "ViewController.h"
//#import "sole-master/sample.cc"

#include <uuid/uuid.h>

#include <iostream>
#include "sole.hpp"

int testMethod()
{
sole::uuid u0 = sole::uuid0(), u1 = sole::uuid1(), u4 = sole::uuid4();

std::cout << "uuid v0 string : " << u0 << std::endl;
std::cout << "uuid v0 base62 : " << u0.base62() << std::endl;
std::cout << "uuid v0 pretty : " << u0.pretty() << std::endl << std::endl;

std::cout << "uuid v1 string : " << u1 << std::endl;
std::cout << "uuid v1 base62 : " << u1.base62() << std::endl;
std::cout << "uuid v1 pretty : " << u1.pretty() << std::endl << std::endl;

std::cout << "uuid v4 string : " << u4 << std::endl;
std::cout << "uuid v4 base62 : " << u4.base62() << std::endl;
std::cout << "uuid v4 pretty : " << u4.pretty() << std::endl << std::endl;

u1 = sole::rebuild("F81D4FAE-7DEC-11D0-A765-00A0C91E6BF6");
u4 = sole::rebuild("GITheR4tLlg-BagIW20DGja");

std::cout << "uuid v1 rebuilt : " << u1 << " -> " << u1.pretty() << std::endl;
std::cout << "uuid v4 rebuilt : " << u4 << " -> " << u4.pretty() << std::endl;

return 0;
}

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

// testMethod();
[self testUUID];
NSLog(@"%@", [self uuid]);
for (int i = 0; i < 100; i++) {
}
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (void)testUUID
{
uuid_t u;
uuid_t ur;
uuid_t ut;

uuid_generate(u);
uuid_generate_random(ur);
uuid_generate_time(ut);

char a[16];
uuid_unparse(u, a);
char b[16];
uuid_unparse(ur, b);
char c[16];
uuid_unparse(ut, c);

// printf("%s\n", a);
// printf("%s\n", b);
printf("%s\n", c);
}

- (NSString *)uuid{
// Create universally unique identifier (object)
CFUUIDRef uuidObject = CFUUIDCreate(kCFAllocatorDefault);

// Get the string representation of CFUUID object.
NSString *uuidStr = (NSString *)CFBridgingRelease(CFUUIDCreateString(kCFAllocatorDefault, uuidObject));

// If needed, here is how to get a representation in bytes, returned as a structure
// typedef struct {
// UInt8 byte0;
// UInt8 byte1;
// ...
// UInt8 byte15;
// } CFUUIDBytes;
// CFUUIDBytes bytes = CFUUIDGetUUIDBytes(uuidObject);

CFRelease(uuidObject);

return uuidStr;
}


@end