331

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?

jscs
  • 63,694
  • 13
  • 151
  • 195
zacjordaan
  • 3,731
  • 2
  • 18
  • 16

8 Answers8

689

Try this one:

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

Swift 3/4/5

let uuid = UUID().uuidString
print(uuid)
Tilak Madichetti
  • 4,110
  • 5
  • 34
  • 52
Ahmed Al Hafoudh
  • 8,281
  • 1
  • 18
  • 34
  • 7
    In XCode 6.1, I found this error: 'UUID()' is unavilable: use object construction 'NSUUID()'. We should change it to NSUUID().UUIDString – ohyes Oct 21 '14 at 13:26
  • The above suggestion is no different than calling `NSUUID.init().UUIDString`, right? – Kyle Redfearn Nov 23 '15 at 17:37
  • 4
    As of Xcode 8 (beta), iOS 10 and Swift 3, this has been renamed to `UUID().uuidString` – califrench Jul 01 '16 at 09:44
  • Mind adding this to the answer? – Ahmed Al Hafoudh Jul 01 '16 at 12:20
  • 1
    Hi I used this method UUID().uuidString but it seems to return different string everytime i use it, I want it to be 1 value only for the app that I install. Any insight? – nerezza Dec 16 '16 at 03:26
  • @nerezza - for getting the same value, use following code: let uuid = UIDevice.current.identifierForVendor?.uuidString it returns same string all time for the same device. – g212gs Apr 26 '17 at 06:57
  • @g212gs - Not exactly, as per the documentation: "The value in this property remains the same while the app (or another app from the same vendor) is installed on the iOS device. The value changes when the user deletes all of that vendor’s apps from the device and subsequently reinstalls one or more of them...... Therefore, if your app stores the value of this property anywhere, you should gracefully handle situations where the identifier changes." - https://developer.apple.com/reference/uikit/uidevice/1620059-identifierforvendor – Zigglzworth May 08 '17 at 10:52
  • @Zigglzworth - Yes, now I got exact meaning, thanks for the clarification :) – g212gs May 10 '17 at 05:12
  • Note that the resultant string is uppercase. This is called out in @Celil's [answer](https://stackoverflow.com/a/47623435/2360257), but figured it'd be worth commenting here since it differs from other languages, and I've been bitten by it. – mway Feb 02 '23 at 18:07
31

You could also just use the NSUUID API:

let uuid = NSUUID()

If you want to get the string value back out, you can use uuid.UUIDString.

Note that NSUUID is available from iOS 6 and up.

James Frost
  • 6,960
  • 1
  • 33
  • 42
24

For Swift 4;

let uuid = NSUUID().uuidString.lowercased()
Celil Bozkurt
  • 1,693
  • 16
  • 18
  • 7
    Graveyard nitpick here but I don't know why lowercase UUID is preferable to a regular UUID. – AlexK Nov 01 '18 at 02:35
  • 3
    Because they are supposed to be lower case. as per spec. UUIDs can be read in either case, but should output lower case. iOS is the only platform that outputs upper case. – Brill Pappin Jun 08 '21 at 17:49
  • 1
    UUIDs are written in base 16 which uses numbers 0-9 and characters a-f. There is no distinction between upper and lowercase letters. However, RCF 4122 https://tools.ietf.org/html/rfc4122#section-3 requires that UUID generators output in lowercase BUT systems accept UUIDs in upper and lowercase. – Daniel Oct 08 '21 at 14:17
17

For Swift 3, many Foundation types have dropped the 'NS' prefix, so you'd access it by UUID().uuidString.

Scott H
  • 1,509
  • 10
  • 14
10

Also you can use it lowercase under below

let uuid = NSUUID().UUIDString.lowercaseString
print(uuid)

Output

68b696d7-320b-4402-a412-d9cee10fc6a3

Thank you !

SwiftDeveloper
  • 7,244
  • 14
  • 56
  • 85
  • 1
    Thanks. it's now `NSUUID().uuidString.lowercased()` – Patrick Dura Dec 21 '17 at 13:17
  • 5
    What is the benefit of using lowercase like this? – Luke Stanyer Apr 02 '18 at 12:58
  • Perhaps it is more pleasing to the eye than uppercase? – tgunr Jan 28 '19 at 15:52
  • 1
    the benefit is that it's supposed to output lower case, as *all other platforms* output lower case based on the spec. Apple is doing it wrong. – Brill Pappin Jun 08 '21 at 17:51
  • UUIDs are written in base 16 which uses numbers 0-9 and characters a-f. There is no distinction between upper and lowercase letters. However, RCF 4122 https://tools.ietf.org/html/rfc4122#section-3 requires that UUID generators output in lowercase BUT systems accept UUIDs in upper and lowercase. – Daniel Oct 08 '21 at 14:16
9

Each time the same will be generated:

if let uuid = UIDevice.current.identifierForVendor?.uuidString {
    print(uuid)
}

Each time a new one will be generated:

let uuid = UUID().uuidString
print(uuid)
iAleksandr
  • 595
  • 10
  • 11
2

UUID is a simple structure, which has the property uuidString. uuidString - returns a string created from the UUID, such as “E621E1F8-C36C-495A-93FC-0C247A3E6E5F”.

UUID is guaranteed to be unique.

Swift code:

let identifier = UUID().uuidString
Swift.print(identifier) // Result: "6A967474-8672-4ABC-A57B-52EA809C5E6D"

Apple official documentation about UUID.
Full article https://tonidevblog.com/posts/how-to-generate-a-random-unique-identifier-with-uuid/

1

Under macOS 13 and Swift 5.7, Foundation's UUID() method does indeed return an upper case UUID, and Vapor Fluent is at least one framework that uses it as is. So, when you create a new record with an id=nil, Fluent will get a new uppercase UUID and store the new record with it. MySQL, on the other hand, generates lowercase UUIDs, and will store the lowercase UUID if you create the record in MySQL. MySQL and SQLite both treat the uppercase and lowercase keys as DIFFERENT. Caveat Emptor.

BUT... if you use the UUID from a previous data read, it shouldn't matter if it was saved with an upper or lower case UUID. You just have to be sure not to change the key's case.

Dr. Mike
  • 11
  • 3