Wesley de Groot's Blog
Identifiable protocol

Back

Identifiable is a protocol in Swift that allows you to uniquely identify instances of a type.

What is Identifiable?

The Identifiable protocol is a protocol in Swift that allows you to uniquely identify instances of a type. It requires a single property, id, which must be unique for each instance. This is particularly useful in SwiftUI, where you often need to differentiate between views in a list.

Why use Identifiable?

Using the Identifiable protocol helps to ensure that each instance of a type can be uniquely identified, which is crucial for many operations in SwiftUI, such as diffing and updating views in a list. By conforming to Identifiable, you can use the item in a ForEach loop or any other context where unique identification is necessary.

Identifiable struct

struct User: Identifiable {
    var id: UUID
    var name: String
}

Identifiable enum

enum Direction: Identifiable {
    var id: Self { return self }

    case north
    case south
    case east
    case west
}

Caveats

Sometimes, you want to use some built-in value types, like a String but they are not Identifiable by default. In such cases, you can create a wrapper type that conforms to Identifiable and contains the value type.

Identifiable String

With this extension you make String conform to Identifiable by providing a unique identifier based on its hash value.

extension String: @retroactive Identifiable {
    /// The identifier of the string.
    ///
    /// This is a hash value of the string to make String conform to Identifiable.
    public var id: Int {
        return hash
    }
}

Identifiable LocalizedStringKey

With this extension you make LocalizedStringKey conform to Identifiable by providing a unique identifier based on its string key.

extension LocalizedStringKey: @retroactive Identifiable {
    /// The identifier of the localized string key.
    ///
    /// This is a random hash value to make LocalizedStringKey conform to Identifiable.
    public var id: Int {
        return self.stringKey?.hashValue ?? UUID().hashValue
    }
}

Identifiable Color

With this extension you make Color conform to Identifiable by providing a unique identifier based on its hash value.

extension Color: @retroactive Identifiable {
    /// The identifier of the color.
    ///
    /// This is a hash value of the color to make Color conform to Identifiable.
    public var id: Int {
        return hash
    }
}

Identifiable Date

With this extension you make Date conform to Identifiable by providing a unique identifier based on the hash value of the description.

extension Date: @retroactive Identifiable {
    /// The identifier of the date.
    ///
    /// This is a hash value of the date to make Date conform to Identifiable.
    public var id: Int {
        return self.description.hashValue
    }
}

Wrap up

In this post, we explored the Identifiable protocol in Swift and how it can be used to uniquely identify instances of a type. We looked at various examples, including struct, enum, and extensions for built-in types like String, LocalizedStringKey, Color, and Date. By conforming to Identifiable, we can leverage the power of unique identification in our SwiftUI applications, making it easier to manage and update views in a list.

Resources:

Read more

Share


Share Bluesky Mastodon Twitter LinkedIn Facebook
x-twitter mastodon github linkedin discord threads instagram whatsapp bluesky square-rss sitemap