Generating UUIDs with Swift

Viewed 8885

I'm trying to generate a UUID using Swift. The official documentation is confusing me a little bit.

The documentation says

init?(uuidString: String) Create a UUID from a string such as “E621E1F8-C36C-495A-93FC-0C247A3E6E5F”.

Does this mean that the string I need to create the UUID from has to be in that format?

For a project I'm doing, I have a string and I want to generate a UUID specifically for that string.

Here's what I tried:

import Foundation

var str = UUID(uuidString: "Hello World")
print(str.uuidString)

This gives me the error,

value of optional type 'UUID?' not unwrapped; did you mean to use '!' or '?'?

I'm still very new to Swift and all this. I don't know what to do in this case.

Basically, I need to generate a UUID SPECIFICALLY for a string (such as "Hello world"), and then I want to use the UUID to return the original string.

2 Answers
Related