How to create a GUID/UUID using iOS
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
On iOS, generating a GUID or UUID is simple with Foundation APIs, but correct usage depends on lifecycle and persistence requirements. Some identifiers should be ephemeral per event, while others must remain stable across app sessions. This guide covers generation, formatting, persistence, and validation patterns in Swift and Objective-C.
Generate UUID Values in Swift
Swift provides UUID, which creates random version-4 UUID values suitable for most application-level identity use cases.
Each call returns a new value. Collision probability is negligible for normal app workloads.
Generate UUID Values in Objective-C
Objective-C uses NSUUID, which is equivalent for practical purposes.
If your project mixes Swift and Objective-C, exchange values as strings at module boundaries and parse into typed UUIDs where possible.
Decide Whether Identifier Should Persist
A common mistake is generating a new identifier every launch even when business logic requires stability. Define the lifetime first.
- Event correlation ID: create new UUID per action.
- Local record primary key: create once per record.
- App instance identifier: generate once and persist.
Persisting a non-sensitive app-scoped identifier can be done with UserDefaults.
If identifier confidentiality matters, use Keychain storage instead.
Normalize Formatting for External Contracts
Backends sometimes require lowercase or compact form without hyphens. Normalize at integration boundaries, not everywhere.
Keeping one internal canonical form avoids comparison and logging inconsistencies.
Keep UUID Typed in Data Models
Using UUID in your model improves type safety and reduces accidental format bugs.
Convert to string only when crossing process or storage boundaries.
Validate Incoming UUID Strings
When accepting identifier input from APIs, deep links, or external systems, validate before use.
Failing early at validation boundaries prevents confusing downstream errors.
Correlation IDs for Logging and Tracing
UUIDs are excellent for request tracing.
Consistent correlation IDs make multi-service debugging much faster.
Common Pitfalls
- Regenerating IDs when a stable persisted identifier is required. Fix: define ID lifetime and persist accordingly.
- Storing sensitive identifiers in plain
UserDefaults. Fix: use Keychain for confidentiality requirements. - Mixing uppercase, lowercase, and compact forms throughout the codebase. Fix: keep a canonical internal representation and normalize at boundaries.
- Treating UUID as an authentication secret. Fix: use proper auth tokens for security and UUID only for identity references.
- Using raw strings in domain models. Fix: keep
UUIDtype in model structs and parse at input points.
Summary
- Use
UUID()in Swift orNSUUIDin Objective-C for iOS GUID generation. - Define whether each UUID should be ephemeral or persisted.
- Normalize format only where external systems demand it.
- Prefer typed
UUIDproperties in app models. - Validate inbound strings early and use correlation IDs for observability.
Related reading
- How to create a Looper thread, then send it a message immediately?
- How to create a multiline UITextfield?
- How to create a release signed apk file using Gradle?
- How to create a toast message in Swift?
- How to create a UIImage with solid color?
- How to create a UIView bounce animation?
- How to create an array with incremented values in Swift?
- How to create an AVD for Android 4.0
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.