I think that there are relatively many scenes where an ID (unique ID) is required to uniquely identify an object in the app. Mainly when you need to associate something with something, I think that is the case.
In such a case, it's easy to think of
However, these methods may not always be sufficient when the degree of parallelism of processing is high.
There is an easy way to generate a unique ID. We use something called UUID.
An identifier that uniquely identifies an object on the software and is represented by a 128-bit numerical value. It is standardized including a generation method that avoids collisions.
When expressed as a character string, it is expressed in hexadecimal notation such as 550e8400-e29b-41d4-a716-446655440000
.
Swift also has a standard function to generate this UUID.
The process of generating a UUID, converting it to a hexadecimal notation string, and printing it can be implemented as follows.
uuidprint.swift
let uuid = UUID()
let uniqueIdString = uuid.uuidString
print(uniqueIdString)
In the above code, ʻuuid` is of type UUID. The UUID is a 128-bit (= 16byte) number. However, in actual handling, I think that there are many cases where you want to use character string notation. The string notation can be obtained as the uuidString property of a UUID type object. (It will be a String type.)
Xcode: 11.7 iOS: 13.7 Swift version: Swift5
that's all
Recommended Posts