DEV Community

Cover image for How to generate a random unique identifier with UUID
Anton Paliakou
Anton Paliakou

Posted on

3 1

How to generate a random unique identifier with UUID

UUID is a universally unique identifier. UUID can be used to identify types, interfaces, and other items.
Creating an object or some entity with an unique identifier is a common task for developers. UUID structure created for to do this and to do it best, do not need create "bicycle".
UUID is guaranteed to be unique. When initializing the structure, a new UUID is created with RFC 4122 version 4 random bytes.

import Foundation

let identifier = UUID().uuidString
Swift.print(identifier) // Result: "6A967474-8672-4ABC-A57B-52EA809C5E6D" 
Enter fullscreen mode Exit fullscreen mode

Available iOS 6.0+, macOS 10.8+, tvOS 9.0+, watchOS 2.0+.

What's UUID from a code perspective?

UUID is a simple structure and is a part of Foundation framework.

public struct UUID: ReferenceConvertible, Hashable, Equatable, CustomStringConvertible {
  /// ...
}
Enter fullscreen mode Exit fullscreen mode

UUID conforms 4 protocols:

Also, the structure has some properties:

1 uuid - Returns the UUID as bytes.

var uuid: (UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8) { get } 
Enter fullscreen mode Exit fullscreen mode

2 uuidString - Returns a string created from the UUID, such as “E621E1F8-C36C-495A-93FC-0C247A3E6E5F”

var uuidString: String { get }
Enter fullscreen mode Exit fullscreen mode

Articles comes to devto portal with delay 1-2 week, if you want read my articles without delay you can visit ToniDevBlog

Article originally published at ToniDevBlog

Thanks for reading! See you soon. 👋

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more