Search code examples
swiftuuid

Verify a string as a valid UUID in Swift


Given an supplied String, how can I verify wether the String is a valid UUID in Swift?

A valid UUID (as far as I know) could be:

33041937-05b2-464a-98ad-3910cbe0d09e
3304193705b2464a98ad3910cbe0d09e

Solution

  • You could use UUID

    var uuid = UUID(uuidString: yourString)
    

    This will return nil if yourString is not a valid UUID

    Note: this only validates the first case you presented, not the second but adding the dashes yourself is trivial.