[ios] 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?

This question is related to ios swift cocoa-touch guid core-foundation

The answer is


Try this one:

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

Swift 3/4/5

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

Similar questions with ios tag:

Similar questions with swift tag:

Similar questions with cocoa-touch tag:

Similar questions with guid tag:

Similar questions with core-foundation tag: