Swift Identifiable

Anonymous contributor's avatar
Anonymous contributor
Published Aug 29, 2023
Contribute to Docs

Identifiable is a protocol in Swift that enforces types to include a unique identifier property. This identifier helps differentiate individual instances of the type from one another within collections or arrays, making it easier to handle, update, and manage specific instances.

  • Learn how to build iOS applications with Swift and SwiftUI and publish them to Apples' App Store.
    • Includes 7 Courses
    • With Certificate
    • Beginner Friendly.
      13 hours
  • A powerful programming language developed by Apple for iOS, macOS, and more.
    • Beginner Friendly.
      12 hours

Syntax

An Identifiable protocol is created like a regular protocol, followed by the Identifiable keyword:

protocol protocolName: Identifiable {
  // Protocol body
}

Example

Here, the Employee struct conforms to the Identifiable protocol by providing a unique id property using the UUID struct. This identifier can be used to distinguish instances of Employee in a collection or array, allowing for easy updates and deletions of specific employees.

struct Employee: Identifiable {
let id = UUID()
let firstName: String
let lastName: String
let isOnVacation: Bool
}

All contributors

Contribute to Docs

Learn Swift on Codecademy

  • Learn how to build iOS applications with Swift and SwiftUI and publish them to Apples' App Store.
    • Includes 7 Courses
    • With Certificate
    • Beginner Friendly.
      13 hours
  • A powerful programming language developed by Apple for iOS, macOS, and more.
    • Beginner Friendly.
      12 hours