4

I try to parse UUID from string but it's always nil. I write it like this.

UUID(uuidString: "my UUID")

I even try it like this in xcode expression watcher but it's also nil

UUID(uuidString: UUID().uuidString)

But when I try NSUUID on the same string, It's working fine.

NSUUID(uuidString: "my UUID") 

Did anyone know what should I check? or is there anyway to convert from NSUUID to UUID?

Thank you very much in advance.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
LLF
  • 668
  • 3
  • 10
  • 27
  • Have you check this : https://stackoverflow.com/questions/39142278/how-to-convert-string-to-nsuuid-in-swift – Dhaval Raval Nov 07 '19 at 14:34
  • 4
    If you are creating a `UUID` from a string, it must be a valid `UUID`. – Sulthan Nov 07 '19 at 14:41
  • @DhavalRaval I want a result as UUID. NSUUID can be initial just fine. – LLF Nov 08 '19 at 01:46
  • @Sulthan Sorry to make you confuse with "my UUID" it was just a sample. the real value was already in format that I think it's valid ("68753A44-4D6F-1226-9C60-0050E4C00067") – LLF Nov 08 '19 at 01:48
  • this code is working in my project.. isn't it? let str : String = UUID().uuidString print(str) let UUIDString = UUID(uuidString: str) print("UUIDString print : \(String(describing: UUIDString))") – Dhaval Raval Nov 08 '19 at 07:28

1 Answers1

6

I try to parse UUID from string but it's always nil. I write it like this.

UUID(uuidString: "my UUID") -> 

reason

The standard format for UUIDs represented in ASCII is a string punctuated by hyphens, for example 68753A44-4D6F-1226-9C60-0050E4C00067.

Returns nil for invalid strings.

I even try it like this in Xcode expression watcher but it's also nil

UUID(uuidString: UUID().uuidString) -> it is returning a UUID like (7AFD7082-7A14-44F8-B839-5C4D98842798), working fine 
Community
  • 1
  • 1
SGDev
  • 2,256
  • 2
  • 13
  • 29
  • Sorry to make you confuse with "my UUID" it was just a sample. the real value was already in format like your example ("68753A44-4D6F-1226-9C60-0050E4C00067"). I don't understand that is there any other condition to make it nil. – LLF Nov 08 '19 at 01:43
  • @LLF if your UUID is in correct format then it never return nil. kindly add your UUID here. – SGDev Nov 08 '19 at 02:20
  • Sorry for late reply, I just found that it just show as nil in "XCode expression watcher". But somehow it has a value when I do a print(......) instead. Thank you very much – LLF Nov 15 '19 at 11:31