A colossal Dreamer: GR鐵塔-天生我材

Swift PropertyWrapper 본문

Development/아이폰

Swift PropertyWrapper

江多林 2022. 7. 11. 11:08

괜찮은 도구: PropertyWrapper

@propertyWrapper public class PublishedBuffer<Buffer, Failure>
where Buffer: RandomAccessCollection & RangeReplaceableCollection, Failure: Error, Buffer.Index == Int {
    public var wrappedValue: Buffer { ... }
    public var projectedValue: PublishedBuffer<Buffer, Failure>.Publisher { ... }
    
    public class Publisher: Combine.Publisher { ... }
}

 

하나의 선언으로 3가지 타입으로 접근이 가능함.

@PublishedBuffer<[Int], Error> var buffer: [Int]

 

아래와 같이 3가지 관점에서 접근이 가능하다.

1. buffer: [Int] // wrappedValue, 심지어 readonly 이다. !!

2. private _buffer: PublisehdBuffer<Int, Error>

3. $buffer: Publisher.Buffer<Int, Error>.Publisher // projectedValue

 

정의된 개체 외부에서도 buffer, $buffer 접근이 가능하다.

_buffer 는 private 으로 자동정의되며, 정의된 개체내부에서만 접근이 가능하다.

 

Sequential load로 버퍼를 채우고, (buffer)

채워진 버퍼를 Combine.Publisher 로 접근하고, ($buffer)

내부버퍼 제어를 위한 PublishedBuffer 접근 동작 (_buffer)

 

하나의 property 의 상이 2개 이상 필요할 때 아름답게 표현하고,

접근할 수 있다. !!

 

끝.!

 

참조:

https://quickbirdstudios.com/blog/swift-property-wrappers/

 

Advanced Property Wrappers in Swift

How to use property wrappers in Swift and SwiftUI to simplify your code and make it more reusable. With lots of practical examples and applications.

quickbirdstudios.com