Shorten the UUID to base64 in Swift.

UUIDs can be generated in a distributed manner and are fairly easy to use, but they are quite long when encoding.

E2731BE6-470D-4782-B853-E5B13E8A1ECF D879A224-9A58-4D71-8E43-B7F3D001B7E4 FFF4DCE4-3C49-459B-BDB9-EB4B5A030FEB

I want to avoid preserving this as it is.

UUIDs are usually written in hexadecimal, so if you save them as strings, you should be able to shorten them even more.

Implementation

Implement ʻextension` like this.


extension UUID {
    public var base64String: String {
        var base64WithPadding = self.data.base64EncodedString()
        //At the end of base64==Get rid of
        base64WithPadding.removeLast(2)
        return base64WithPadding
    }
    
    public var data: Data {
        var result = Data()
        let uuidTuple = self.uuid
        result.append(uuidTuple.0)
        result.append(uuidTuple.1)
        result.append(uuidTuple.2)
        result.append(uuidTuple.3)
        result.append(uuidTuple.4)
        result.append(uuidTuple.5)
        result.append(uuidTuple.6)
        result.append(uuidTuple.7)
        result.append(uuidTuple.8)
        result.append(uuidTuple.9)
        result.append(uuidTuple.10)
        result.append(uuidTuple.11)
        result.append(uuidTuple.12)
        result.append(uuidTuple.13)
        result.append(uuidTuple.14)
        result.append(uuidTuple.15)
        return result
    }
}

This can reduce 36 characters to 22 characters.

40CC21EA-7D27-49CC-8A18-280373933BE2 -> QMwh6n0nScyKGCgDc5M74g
2AF87112-E64C-441F-85E1-FD4FCCD097CA -> KvhxEuZMRB+F4f1PzNCXyg
D9C051E6-D7E2-4198-9CB1-A399E347D860 -> 2cBR5tfiQZicsaOZ40fYYA

test

Test with the code below

for _ in 0..<10 {
    let id = UUID()
    let data = Data(base64Encoded: id.base64String + "==")!

    let uuid = UUID(uuid: (
        data[0], data[1], data[2], data[3], data[4], data[5],
        data[6], data[7], data[8], data[9], data[10], data[11],
        data[12], data[13], data[14], data[15]
    ))

    print(id, "->", id.base64String, "->", uuid)
}
40CC21EA-7D27-49CC-8A18-280373933BE2 -> QMwh6n0nScyKGCgDc5M74g ->
40CC21EA-7D27-49CC-8A18-280373933BE2

2AF87112-E64C-441F-85E1-FD4FCCD097CA -> KvhxEuZMRB+F4f1PzNCXyg -> 
2AF87112-E64C-441F-85E1-FD4FCCD097CA

D9C051E6-D7E2-4198-9CB1-A399E347D860 -> 2cBR5tfiQZicsaOZ40fYYA -> 
D9C051E6-D7E2-4198-9CB1-A399E347D860

Lossless compression is possible.

Recommended Posts

Shorten the UUID to base64 in Swift.
How to add sound in the app (swift)
[Swift] Use UserDefaults to save data in the app
Pass the i18n locale to JavaScript
I want to remove the top margin in Grouped UITableView (swift)
[Swift] Copy the character string to the clipboard
[swift5] How to specify color in hexadecimal
Java reference to understand in the figure
How to do base conversion in Java
[Swift] How to fix Label in UIPickerView
[Swift] How to implement the countdown function
How to get the date in java
How to overwrite Firebase data in Swift
[Swift] How to pass the Label value in the selected collectionViewCell to the destination TextField
[Swift] How to set an image in the background without using UIImageView.
[Swift] How to get the number of elements in an array (super basic)
How to check the logs in the Docker container
[Swift 5] Processing to close the keyboard on UITableView
I tried to organize the session in Rails
3 ways to import the library in Android Studio
How to get the ID of a user authenticated with Firebase in Swift
[Swift] How to implement the LINE login function
When the server fails to start in Eclipse
[swift5] How to implement the Twitter share function
[Swift] How to link the app with Firebase
[Swift] How to change the order of Bar Items in Tab Bar Controller [Beginner]
[Swift] How to implement the fade-in / out function
Add classpath: to the path specified in spring.datasource.schema
[Note] About the introduction to Swift practice Chapter 11
How to execute tasks in parallel in Swift in Swift Package
How to build the simplest blockchain in Ruby
How to check Rails commands in the terminal
I want to get the value in Ruby
[Swift] How to simply describe dismiss that was not taught in the introductory book
How to check if the characters entered in the Swift Text Field are email addresses
[Swift] How to display the entered characters in Widget via UserDefaults when using WidgetKit
I was addicted to the NoSuchMethodError in Cloud Endpoints
How to implement UICollectionView in Swift with code only
How to set the display time to Japan time in Rails
[Java] How to omit the private constructor in Lombok
I tried to organize the cases used in programming
I want to embed any TraceId in the log
Tokoro I rewrote in the migration from Wicket 7 to 8
[Swift] Save Color to User Defaults in Swift UI (memo)
Understand the characteristics of Scala in 5 minutes (Introduction to Scala)
Source used to get the redirect source URL in Java
How to create a data URI (base64) in Java
Organized how to interact with the JDK in stages
Java classes and instances to understand in the figure
How to specify the resource path in HTML import
Find the approximate value of log (1 + x) in Swift
If you want to recreate the instance in cloud9
How to debug the generated jar file in Eclipse
I tried to implement the Euclidean algorithm in Java
[Swift] How to get the document ID of Firebase
[Rails] How to display an image in the view
Compare objects in Swift
To debug in eclipse
Division becomes 0 in Swift
Multidimensional array in Swift
How to get the class name / method name running in Java