Swift) Identifiable 프로토콜

2023. 7. 4. 13:34·앱/Swift

애니메이션 처리를 하던 도중 계속해서 애니메이션이 @State 의 변경에도 불구하고 변경되지 않는 것을 확인하엿다. 왜그런가 해서 다른사람들이 처리한 애니메이션을 봤는데 다른사람은 AnimationHash라는 클래스를 설정해서 애니메이션이 변경되도록 처리해주어서 해당 코드를 분석한 결과 Identifiable이라는 프로토콜을 채택하는것으로 문제를 해결했다고 생각이 들어서 이에 대해서 정리해본다.

identifiable

identifiable이란 프로로토콜로, instance 가 stable identity를 가지도록 하는 프로토콜이다.

여기에서 말하는 stable identity란, being able to see yourself as same person in the past, and future로, 생성된 인스턴스들이 분명하게 서로 다른 값이라는 비교가 가능하도록 하는 프로퍼티를 의미한다.

identifiable 프로토콜 형태

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 }
}

identifiable은 Hashable한 id값을 가지게 된다.

여기에서 가지는 id값은 유니크한 값이다.

즉 identifiable이란 유니크한 id값을 강제할수 있는 프로토콜이다.

예시

Struct AnimationHash: identifiable {
	var id = UUID()
	var animation: Animation
}

여기에서 identifiable을 채택하는것으로 id라는 프로퍼티를 가져야하며, 해당 id값은 uuid()를 통해서 고유의 값을 가지게 된다. (여기에서 uuid를 string으로 주고싶다면, UUID().uuidString 으로 주면된다.)

이러한 방식을 통해서 AnimationHash의 인스턴스는 생성될때 마다 고유한 완전한 다른 인스턴스라는것을 uuid를 통해서 보장받게 된다


참고자료

https://ios-development.tistory.com/m/585https://ios-development.tistory.com/m/585


Uploaded by N2T

반응형

'앱 > Swift' 카테고리의 다른 글

Swift) Protocol과 Associated type  (0) 2023.07.04
Swift) GCD(Grand Central Dispatch) 에 대한 이해 (feat. DispatchQueue, Runloop  (0) 2023.07.04
Swift) Escaping Closure  (0) 2023.07.04
UIKit) UISearchController 를 사용하여 TableView를 생성해보자  (0) 2023.07.04
UIKit) Pull Down Button 과 Pop Up Button  (1) 2023.07.04
'앱/Swift' 카테고리의 다른 글
  • Swift) Protocol과 Associated type
  • Swift) GCD(Grand Central Dispatch) 에 대한 이해 (feat. DispatchQueue, Runloop
  • Swift) Escaping Closure
  • UIKit) UISearchController 를 사용하여 TableView를 생성해보자
진ddang
진ddang
안녕하세요 진땅의 개발자 블로그 입니다. 피드백은 환영입니다. 깃헙 : https://github.com/it794613
    반응형
  • 진ddang
    맛동산이
    진ddang
  • 전체
    오늘
    어제
    • 분류 전체보기 (199)
      • 일기 그리고 목표 (1)
      • 웹 (20)
        • 리액트 (19)
      • 앱 (115)
        • Swift 문법 (15)
        • Swift (65)
        • SwiftUI (32)
        • 리액트 네이티브 (3)
      • CS (30)
        • 컴퓨터그래픽스 (8)
        • 운영체제 (6)
        • 네트워크 (16)
      • 알고리즘 (13)
        • 백준 (12)
        • 프로그래머스 (1)
      • 대외활동 (7)
        • ict한이음(2022.04) (2)
        • 멋쟁이 사자처럼 (5)
        • Apple Developer Academy (0)
      • 다양한 내용들 (8)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    스위프트
    백준
    멋쟁이사자처럼
    리액트
    composable architecture
    SwiftUI
    대외활동
    ReactorKit
    알고리즘
    uikit
    spritekit
    영남대
    Protocol
    컴퓨터그래픽스
    TCA
    widgetkit
    웹
    멋사
    Swift
    dispatchqueue
    cs
    widget
    c++
    swift concurrency
    후기
    문법
    운영체제
    네트워크
    스유
    위젯킷
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
진ddang
Swift) Identifiable 프로토콜
상단으로

티스토리툴바