Swift Identifiable
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.
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: Stringlet lastName: Stringlet isOnVacation: Bool}
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve 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