Check string as valid UUID in Swift

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

A valid UUID (to my knowledge) may be:

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

+5
source share
1 answer

You can use NSUUID

 var uuid = NSUUID(uuidString: yourString) 

This will return nil if yourString not a valid UUID

Note: this confirms only the first case that you presented, and not the second, but adding a hyphen is trivial.

+13
source

Source: https://habr.com/ru/post/1213185/


All Articles