What is a UUID and why is it needed in an iOS app?
\n\nImagine you are creating a notes app. Each note needs a unique "passport" to distinguish it from others. In the programming world, such a passport is a UUID (Universally Unique Identifier). In iOS development with Swift, UUID generation is a basic but critically important task that every developer solves sooner or later.
\n\nOn Stack Overflow, the question "Generate a UUID on iOS from Swift" has garnered thousands of views. Why? Because even experienced programmers sometimes get confused: which class to use? Do I need to import additional libraries? Which UUID is considered "correct"?
\n\nIn this article, we will break everything down from scratch. You will learn how to generate a UUID on iOS using Swift, understand the difference between UUID versions, and learn how to apply them in practice. Let's go!
\n\nBasics: How to generate a UUID in Swift in one line
\n\nThe easiest way to get a UUID in iOS is to use the built-in UUID class from the Foundation framework. It is available "out of the box" and requires no additional imports.
Basic example
\n\nimport Foundation\n\nlet myUUID = UUID()\nprint(myUUID.uuidString)\n// Output: "E621E1F8-C36C-495A-93FC-0C247A3E6E5F"\n\nThat's it! You have just generated a UUID version 4 — a random identifier that is overwhelmingly likely to be unique across the entire universe. Note the uuidString property: it returns a string in the standard format XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX.
What's inside a UUID?
\n\nThe UUID object in Swift stores a 128-bit value. You can get it in its raw form:
let uuid = UUID()\nlet uuidBytes = uuid.uuid // (UInt8, UInt8, ..., UInt8) — a tuple of 16 bytes\nprint(uuidBytes.0) // First byte\n\nBut in practice, you most often work with the string representation.
\n\nUUID from a string: the reverse operation
\n\nSometimes you already have a UUID string (for example, from a database or API) and need to create a UUID object to work with. Swift allows this through an initializer:
let uuidString = "550e8400-e29b-41d4-a716-446655440000"\nif let uuid = UUID(uuidString: uuidString) {\n print("Successfully created UUID: \\(uuid)")\n} else {\n print("Invalid UUID format")\n}\n\nNote: the initializer returns an optional value. If the string does not match the UUID format, you will get nil. This protects your code from invalid data.
Comparing UUIDs: how to check identity
\n\nSwift implements the Equatable protocol for UUID, so you can compare them directly:
let uuid1 = UUID()\nlet uuid2 = UUID()\nlet uuid3 = uuid1\n\nprint(uuid1 == uuid2) // false (different UUIDs)\nprint(uuid1 == uuid3) // true (same object)\n\nThis is convenient for finding duplicates or checking if an identifier exists in a collection.
\n\nUUID as a key in dictionaries and sets
\n\nThe UUID class also implements the Hashable protocol, allowing it to be used as a key in dictionaries and as an element in sets:
var userSessions: [UUID: String] = [:]\nlet sessionId = UUID()\nuserSessions[sessionId] = "Active user session"\n\nlet allIds: Set<UUID> = [UUID(), UUID(), UUID()]\nprint(allIds.count) // 3\n\nThis is a great way to organize data if you need guaranteed unique keys.
\n\nWhat versions of UUID exist? (And why it matters to you)
\n\nThe UUID standard defines several versions. In iOS with Swift, UUID version 4 (random) is generated by default. But what about other versions?
\n\n- \n
- UUID v1 — based on time