Identifiable

no minho·2023년 9월 20일

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
}

리스트를 구성하기 위한 프로토콜로써 많이 사용한다.

출처: 김종권님 ios 블로그
https://ios-development.tistory.com/585

0개의 댓글