[Swift] How to generate an ID to uniquely identify a certain thing (using UUID)

Thing you want to do

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.

What is 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.

How to generate 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.)

Operation check environment

Xcode: 11.7 iOS: 13.7 Swift version: Swift5

that's all

Recommended Posts

[Swift] How to generate an ID to uniquely identify a certain thing (using UUID)
How to generate a primary key using @GeneratedValue
[Swift] How to send a notification
How to execute a contract using web3j
How to return a value from Model to Controller using the [Swift5] protocol
[Swift5] How to create a splash screen
[Swift5] How to implement animation using "lottie-ios"
[Swift] How to set an image in the background without using UIImageView.
How to get the ID of a user authenticated with Firebase in Swift
How to create an Excel form using a template file with Spring MVC
[Rails] How to create a graph using lazy_high_charts
How to delete a controller etc. using a command
[Ethereum] How to execute a contract using web3j-Part 2-
[Ruby] How to generate a random alphabet string
How to automatically generate a constructor in Eclipse
How to use an array for a TreeMap key
How to manually generate a JWT with Rails Knock
How to delete custom Adapter elements using a custom model
How to generate / verify ID token in Java Memo
How to convert A to a and a to A using AND and OR in Java
[Swift] How to get the document ID of Firebase
[Ruby/Rails] How to generate a password in a regular expression
[Rails] How to install a decorator using gem draper
How to connect a container created later to an existing network
[Swift] Table View Cell, how to jump to an external URL
[iOS] [Objective-C] How to update a widget from an Objective-C app
How to change a string in an array to a number in Ruby
[2020 version] How to send an email using Android Studio Javamail
How to output array values without using a for statement
How to make an app using Tensorflow with Android Studio
How to join a table without using DBFlute and sql
How to register as a customer with Square using Tomcat
How to get the latest live stream ID for a channel without using the YouTube Data API