本文給ios開發者講解一個小技巧,如何使用Objective-C建立UUID的代碼。
UUID含義是通用唯一識别碼 (Universally Unique Identifier),這 是一個軟體建構的标準,也是被開源軟體基金會 (Open Software Foundation, OSF) 的組織在分布式計算環境 (Distributed Computing Environment, DCE) 領域的一部份。
UUID 的目的,是讓分布式系統中的所有元素,都能有唯一的辨識資訊,而不需要透過中央控制端來做辨識資訊的指定。如此一來,每個人都可以建立不與其它人沖突的 UUID。在這樣的情況下,就不需考慮資料庫建立時的名稱重複問題。目前最廣泛應用的 UUID,即是微軟的 Microsoft's Globally Unique Identifiers (GUIDs),而其他重要的應用,則有 Linux ext2/ext3 檔案系統、LUKS 加密分割區、GNOME、KDE、Mac OS X 等等。
- (NSString *)createUUID
{
// Create universally unique identifier (object)
CFUUIDRef uuidObject = CFUUIDCreate(kCFAllocatorDefault);
// Get the string representation of CFUUID object.
NSString *uuidStr = [(NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuidObject) autorelease];
// 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;
}
本文轉自 wws5201985 51CTO部落格,原文連結:http://blog.51cto.com/wws5201985/736042,如需轉載請自行聯系原作者