Property Wrapper
- A property wrapper adds a layer of separation between code that manages how a property is stored and the code that defines a property.
 - For example, if you have properties that provide thread-safety checks or store their underlying data in a database, you have to write that code on every property. When you use a property wrapper, you write the management code once when you define the wrapper, and then reuse that management code by applying it to multiple properties.
 - ์ฌ๋ฌ ํ๋กํผํฐ๋ค์ ๋์ผํ ๋ก์ง์ ์คํํด์ผํ๋๊ฒฝ์ฐ ํ๋กํผํฐ ๋ํผ๋ฅผ ์ ์ํ๊ณ ์ฌ๋ฌ ํ๋กํผํฐ๋ค์ ํ๋กํผํฐ ๋ํผ๋ฅผ ์ ์ฉํ์ฌ ํด๋น ๋ก์ง์ ์ฌ์ฌ์ฉํ ์ ์๋ค.
 
์ ์ํ๊ธฐ
- To define a property wrapper, you make a structure, enumeration, or class that defines a wrappedValue property.
 - The setter ensures that new values are less than or equal to 12, and the getter returns the stored value.
 
@propertyWrapper
struct TwelveOrLess {
    private var number = 0
    var wrappedValue: Int {
        get { return number }
        set { number = min(newValue, 12) }
    }
}
์ฌ์ฉํ๊ธฐ
- You apply a wrapper to a property by writing the wrapper’s name before the property as an attribute.
 - a structure that stores a rectangle that uses the TwelveOrLess property wrapper to ensure its dimensions are always 12 or less
 
struct SmallRectangle {
    @TwelveOrLess var height: Int
    @TwelveOrLess var width: Int
}
var rectangle = SmallRectangle()
print(rectangle.height)
// Prints "0"
rectangle.height = 10
print(rectangle.height)
// Prints "10"
rectangle.height = 24
print(rectangle.height)
// Prints "12"
์ด๊ธฐ๊ฐ ์ค์ 
- SmallRectangle์ ์ด๊ธฐ๊ฐ ์ค์ ์ ์ง์ํ๊ณ ์ถ์ผ๋ฉด property wrapper ์ ์ด๋์ ๋ผ์ด์ ๋ฅผ ์ถ๊ฐํด์ค์ผํ๋ค
 
@propertyWrapper
struct TwelveOrLess {
    private var number = 0
    var wrappedValue: Int {
        get { return number }
        set { number = min(newValue, 12) }
    }
    
    init() {
        
    }
    
    init(wrappedValue: Int) {
        number = min(wrappedValue, 12)
    }
}
struct SmallRectangle {
    @TwelveOrLess var height: Int = 2
    @TwelveOrLess var width: Int
}
var rectangle = SmallRectangle()
print(rectangle.height)
// Prints "2"
print(rectangle.width)
// Prints "0"
์ฐธ๊ณ ์๋ฃ
https://eunjin3786.tistory.com/472
https://docs.swift.org/swift-book/LanguageGuide/Properties.html#ID617
'iOS ๐ > Swift' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| Swift) ์ฐ์ฐ์ == ์ === ์ ์ฐจ์ด (0) | 2022.09.24 | 
|---|---|
| self vs Self (0) | 2022.04.27 | 
| RxSwift Operator : Map, FlatMap (0) | 2022.04.18 | 
| typealias, associatedtype (0) | 2022.04.15 | 
| Non-Escaping Closure ์ Escaping Closure (0) | 2021.12.28 |