# 属性包装器

属性包装器在管理属性如何存储和定义属性的代码之间添加了一个分隔层。

`属性包装器只能应用于变量`

**定义属性包装器**

需要使用`@PropertyWrapper`定义一个包装器

```swift
@propertyWrapper
struct TwelveOrLess {
    private var number: Int    
    init() { self.number = 0 }
    var wrappedValue: Int {
        get { return number }
        set { number = min(newValue, 12) }
    }
}

```

**使用属性包装器**

在定义属性时，使用属性包装器

```swift

// 使用@TwelveOrLess修饰属性 

struct SmallRectangle {
    @TwelveOrLess var height: Int
    @TwelveOrLess var width: Int
}

var rectangle = SmallRectangle()
print(rectangle.height)
// 打印 "0"

rectangle.height = 10
print(rectangle.height)
// 打印 "10"

rectangle.height = 24
print(rectangle.height)
// 打印 "12"

```

`TwelveOrLess`定义了一个包装器，包装器中`number`用于保存属性的值(定义成private，阻止其他类访问)。

`wrappedvalue`计算属性用于对`number`的读写，同时在读写时添加额外的操作。

`@TwelveOrLess`修饰需要包装器的属性，对于属性的读写就是对`TwelveOrLess`中`wrappedvalues`的读写。

## 设置被包装属性的初始值

被包装属性在初始化时，会调用包装器的构造器。如果需要为包装器提供更复杂的操作，则需要添加更多的构造器。

* 如果没有为被包装属性提供初始值，则会调用包装器的默认构造器。
* 当为被包装属性提供初始值，但是需要在定义包装器时添加一个构造器。

```swift

@propertyWrapper
struct SmallNumber {
    private var maximum: Int
    private var number: Int

    var wrappedValue: Int {
        get { return number }
        set { number = min(newValue, maximum) }
    }

    init() {
        maximum = 12
        number = 0
    }
    init(wrappedValue: Int) {
        maximum = 12
        number = min(wrappedValue, maximum)
    }
    init(wrappedValue: Int, maximum: Int) {
        self.maximum = maximum
        number = min(wrappedValue, maximum)
    }
}


struct ZeroRectangle {

    // 未提供任何初始值，调用包装器的默认构造器
    @SmallNumber var height: Int

    // 提供初始值，调用init(wrappedValue: Int)构造器
    @SmallNumber var width: Int = 13

    // 可以显式调用某个构造器
    @SmallNumber(wrappedValue:13, maximum: 10) var zValue : Int
}

```

## 3. 注意点

* 被包装后的属性事实上已经不是原本的类型了，而是一个包装器结构体类型的属性
* 属性包装器本身就是一个结构体，需要定义一个`wrappedValue`属性(对于被包装属性的读写就是对`wrappedValue`属性的读写)。
* 被包装属性的初始化将会调用属性包装器的构造器。
* 属性包装器只能应用于变量

> 属性包装器是一个语法糖，被属性包装器修饰的属性实际上类型是属性包装器的结构体类型；对被包装属性的读写就转换为对包装器实例中`wrappedValue`属性的读写;而在包装器的结构体中，可以提供更多复杂的逻辑。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gitbook.existorlive.cn/kai-fa-yu-yan-xue-xi/swift/9.-lei-he-jie-gou-ti/shu-xing/2.3-shu-xing-bao-zhuang-qi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
