Transferable

2025. 12. 14. 16:11·Programing Langauge/swift
반응형

🎥 Transferable

 

✅ 정의

 

기존에는 데이터 전달 방식이 제각각, 코드가 복작합고 타입이 안전하지 않음

 

  • Drag & Drop → NSItemProvider
  • Photos → loadTransferable
  • Share → 각자 다른 API

 

 

 

 

🧩 역할

 

  • 타입 안전환 데이터 변환
  • 여러 전송 메커니즘을 하나의 추상화로 통합
이 프로토콜을 채택하는 타입은 어떤 형식(UTType)으로 어떻게
encode /decode 될 수 있는 지 시스템에게 알려주는 역할

 

 

🧱 구조

public protocol Transferable {

    /// import / export할 item 정보를 다음 객체
    associatedtype Representation : TransferRepresentation

	/// @resultBuilder 형식으로, 여러개를 쉽게 정의
    @TransferRepresentationBuilder<Self> static var transferRepresentation: Self.Representation { get }
}
public protocol TransferRepresentation<Item> : Sendable {

    /// import/export 될 타입
    associatedtype Item : Transferable

    /// representation 객체
    associatedtype Body : TransferRepresentation

	// @resultBuilder를 이용하여, 
    @TransferRepresentationBuilder<Self.Item> var body: Self.Body { get }
}

 

 

🚀 특징

 

대표적인 TransferRepresentation 객체들을 다음과 같다.

 

DataRepresentation

  • 메모리 기반 (Data)
  • Copy / Paste, Share에 주로 사용
  • 작은 데이터에 적합

FileRepresentation

  • 파일 기반(URL)
  • 이미지, 동영상, PDF 등
  • PhotoPicker, Drag & Drop 등

CodableRepresentation

  • Codable 타입을 자동으로 Data로 치환
  • JSON, plist 등

 

🧪 예시

struct Movie: Transferable {
  let url: URL

  static var transferRepresentation: some TransferRepresentation {
    FileRepresentation(
      contentType: .movie,
      exporting: { movie in
        SentTransferredFile(movie.url) // 밖으로 나갈 때
      },
      importing: { received in
        let copy = URL.documentsDirectory.appending(path: "movie.mp4")

		// 덮어쓰기
        if FileManager.default.fileExists(atPath: copy.path()) {
          try FileManager.default.removeItem(at: copy)
        }

		// Movie 인스턴스로 리턴
        try FileManager.default.copyItem(at: received.file, to: copy)
        return Self.init(url: copy)
      }
    )
  }
}

출처

https://developer.apple.com/documentation/coretransferable/transferable

 

Transferable | Apple Developer Documentation

A protocol that describes how a type interacts with transport APIs such as drag and drop or copy and paste.

developer.apple.com

 

반응형

'Programing Langauge > swift' 카테고리의 다른 글

@DynamicProperty  (0) 2025.10.24
@resultBuilder  (0) 2025.10.22
@autoclosure  (0) 2025.10.10
rethrows  (0) 2025.09.22
@retroactive  (0) 2025.08.23
'Programing Langauge/swift' 카테고리의 다른 글
  • @DynamicProperty
  • @resultBuilder
  • @autoclosure
  • rethrows
Hamp
Hamp
남들에게 보여주기 부끄러운 잡다한 글을 적어 나가는 자칭 기술 블로그입니다.
  • Hamp
    Hamp의 분리수거함
    Hamp
  • 전체
    오늘
    어제
    • 분류 전체보기 (304)
      • CS (30)
        • 객체지향 (2)
        • Network (7)
        • OS (6)
        • 자료구조 (1)
        • LiveStreaming (3)
        • 이미지 (1)
        • 잡다한 질문 정리 (0)
        • Hardware (2)
        • 이론 (6)
        • 컴퓨터 그래픽스 (0)
      • Firebase (3)
      • Programing Langauge (37)
        • swift (32)
        • python (4)
        • Kotlin (1)
      • iOS (132)
        • UIKit (37)
        • Combine (1)
        • SwiftUI (32)
        • Framework (7)
        • Swift Concurrency (22)
        • Tuist (6)
        • Setting (11)
        • Modularization (1)
        • Instruments (6)
      • PS (59)
        • 프로그래머스 (24)
        • 백준 (13)
        • LeetCode (19)
        • 알고리즘 (3)
      • Git (18)
        • 명령어 (4)
        • 이론 (2)
        • hooks (1)
        • config (2)
        • action (7)
      • Shell Script (2)
      • Linux (6)
        • 명령어 (5)
      • Spring (13)
        • 어노테이션 (1)
        • 튜토리얼 (11)
      • CI-CD (4)
      • Android (0)
        • Jetpack Compose (0)
  • 블로그 메뉴

    • 홈
    • 태그
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    백준
    UIKit
    property
    Swift
    dispatch
    프로그래머스
    CS
    dp
    concurrency
    투포인터
    Tuist
    lifecycle
    AVFoundation
    protocol
    IOS
    boostcamp
    SwiftUI
    GIT
    dfs
    Spring
  • 최근 댓글

  • 최근 글

  • 반응형
  • hELLO· Designed By정상우.v4.10.0
Hamp
Transferable
상단으로

티스토리툴바