How to Generate a UUID in Swift
Foundation's UUID struct handles UUID generation in Swift out of the box — call UUID() to get a random, RFC 4122-compliant v4 identifier with no package manager and no configuration. The kotlin uuid page covers UUID primary keys in Exposed table definitions and JPA annotations in Spring.
import Foundation
let myUUID = UUID()
print(myUUID.uuidString)
// Output: 550E8400-E29B-41D4-A716-446655440000
Explanation
import Foundationis the only import needed —UUIDships in Apple's standard framework on every platform (iOS, macOS, watchOS, tvOS).UUID()generates a random UUID (version 4) using the system's secure random source, with no external package required.myUUID.uuidStringreturns the standard 36-character hyphenated representation, uppercase by default.UUIDis a value type (astruct), so it's automaticallyHashableandEquatable, and safe to pass around without reference-counting overhead.
Convert a String to a UUID in Swift
Sometimes a UUID arrives as a string — from a network response, a config file, or user input — and you need to parse it back into a proper UUID value. Swift's UUID(uuidString:) initializer is failable, returning an optional UUID? instead of throwing, so invalid input never crashes your app. The powershell uuid reference explains the difference between [System.Guid]::NewGuid() and [guid]::NewGuid().
import Foundation
let uuidString = "550e8400-e29b-41d4-a716-446655440000"
guard let parsedUUID = UUID(uuidString: uuidString) else {
fatalError("Invalid UUID string")
}
print(parsedUUID.uuidString)
Explanation
UUID(uuidString:)is a failable initializer — it returnsUUID?, never throws, so you must unwrap the result withif letorguard let.- The initializer accepts UUIDs in the standard 8-4-4-4-12 hyphenated format and returns
nilfor malformed strings instead of crashing. - Parsing is case-insensitive — both uppercase and lowercase hex digits are accepted.
- Use this pattern to validate UUIDs coming from JSON payloads, URL path parameters, or any untrusted external input before trusting them.
Other Options for Generating UUIDs in Swift
Foundation's UUID only generates version 4 identifiers — there's no built-in support for v1 or v7. Developers who need other UUID versions typically reach for a third-party Swift package from the Swift Package Index, or implement RFC 9562's v7 algorithm manually.
import Foundation
// NSUUID is the Objective-C-bridged equivalent of UUID,
// useful for interop with older Objective-C codebases
let legacyUUID = NSUUID()
let bridgedUUID = legacyUUID as UUID
print(bridgedUUID.uuidString)
Explanation
- Foundation covers only UUID v4 — there's no
UUID.v1()orUUID.v7()equivalent in the standard library. - For time-sortable v7 UUIDs, developers either add a third-party package from the Swift Package Index or implement the RFC 9562 v7 layout (Unix millisecond timestamp plus random bits) by hand.
NSUUIDis the Objective-C class thatUUIDbridges to automatically — reach for it only when interoperating with legacy Objective-C APIs that expectNSUUIDdirectly.- Since
UUIDandNSUUIDbridge seamlessly withas, new Swift code rarely needs to instantiateNSUUIDdirectly.