Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate a UUID on iOS from Swift

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?

like image 290
zacjordaan Avatar asked Jun 26 '14 10:06

zacjordaan


People also ask

How is UUID generated Swift?

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.

What is UUID in iOS Swift?

A universally unique value to identify types, interfaces, and other items.

What is UUID () Swiftui?

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.


1 Answers

Try this one:

let uuid = NSUUID().uuidString print(uuid) 

Swift 3/4/5

let uuid = UUID().uuidString print(uuid) 
like image 146
Ahmed Al Hafoudh Avatar answered Sep 27 '22 19:09

Ahmed Al Hafoudh