In my iOS Swift app I want to generate random UUID (GUID) strings for use as a table key, and this snippet appears to work:
let uuid = CFUUIDCreateString(nil, CFUUIDCreate(nil))
Is this safe?
Or is there perhaps a better (recommended) approach?
In Swift, we can generate UUIDs with the UUID struct. The UUID() initializer generates 128 random bits. Because the UUID struct conforms to the CustomStringConvertible, we can print it as a string.
A universally unique value to identify types, interfaces, and other items.
Swift version: 5.6. A UUID is a universally unique identifier, which means if you generate a UUID right now using UUID it's guaranteed to be unique across all devices in the world.
Try this one:
let uuid = NSUUID().uuidString print(uuid)
Swift 3/4/5
let uuid = UUID().uuidString print(uuid)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With