관리 메뉴

김종권의 iOS 앱 개발 알아가기

[iOS - swift] Identifiable 프로토콜 본문

iOS 기본 (swift)

[iOS - swift] Identifiable 프로토콜

jake-kim 2021. 6. 29. 00:49

Identifiable 프로토콜

  • iOS 13+ (swift5.1)
  • 단순히 id 프로퍼티를 가지고 있는 형태
  • 어떤 struct, class를 정의할 때 ID값이 필요한 경우 해당 protocol을 conform
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
public protocol Identifiable {

    /// A type representing the stable identity of the entity associated with
    /// an instance.
    associatedtype ID : Hashable

    /// The stable identity of the entity associated with this instance.
    var id: Self.ID { get }
}

예시)

  • Student들을 구별할 때 ID값 필요
  • 프로토콜에 있는 프로퍼티 'id' 정의를 강제화
struct Student: Identifiable {
    let id: String
    let name: String
}

* 참고

https://developer.apple.com/documentation/swift/identifiable

Comments