> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/ios/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/ios/swift/type/prop/computed-properties.md).

# computed property (get/set)

accessor╱getter/setter╱🚧 -> keyword "let"

[Swift](/ios/swift.md) ⟩ [type](/ios/swift/type.md) ⟩ [property](/ios/swift/type/prop.md) ⟩ computed property

{% hint style="success" %}
:scales: [stored vs. computed](/ios/swift/type/prop/stored-vs.-computed.md)

* 屬性<mark style="color:yellow;">沒有實際儲存的值</mark>，值是動態計算的。

*<mark style="color:purple;">computed property</mark>* 的優勢：

* <mark style="color:blue;">`get`</mark> ：可<mark style="color:yellow;">動態計算</mark>並傳回屬性值。
* <mark style="color:blue;">`set`</mark> ：可<mark style="color:yellow;">攔截屬性變更</mark>，檢查或調整其值，保護內部[狀態](/ios/master/term/state.md)。

:scales: [willSet/didSet vs. get/set](/ios/swift/type/prop/willset-didset-vs.-get-set.md)
{% endhint %}

{% hint style="info" %}

* *<mark style="color:purple;">computed property</mark>* 也稱為 *<mark style="color:purple;">accessor</mark>*。
* *<mark style="color:purple;">computed property</mark>* 一定要用 <mark style="color:purple;">`var`</mark> 宣告。
  {% endhint %}

{% tabs %}
{% tab title="💈範例" %}

```swift
struct Circle {

    var radius: Double
    
    // ⭐️ computed property
    var area: Double { .pi * radius * radius }    // ⭐️ (implicit) get only
}

// 利用 computed property 來確保屬性值的範圍
struct NoLessThanThree {

    // inner state
    private var _value: Int = 3

    // ⭐️ computed property
    var value: Int {
        get { _value }
        set { _value = max(newValue, 3) }    // 確保數值不小於 3
    }
}

// 註：如果初始化後屬性值就不變，可考慮以下做法：
struct Example {

    // (stored property) private set/internal get (get OK/no set!)
    private(set) var value: Int

    init(value: Int) {
        self.value = max(value, 3)          // 確保數值不小於 3
    }
}
```

📁 測試

```swift
// 測試
var num = NoLessThanThree()
num.value = 5      
num.value = 2      // value 被 setter 調整為 3 ⭐️ 
print(num.value)   // 3
```

{% endtab %}

{% tab title="📗 參考" %}

* [ ] [Computed properties in Swift](https://matteomanferdini.com/computed-property-swift/): A basic feature for safer and cleaner code
* [ ] Develop with Swift ⟩ [Customize views with properties](https://developer.apple.com/tutorials/develop-in-swift/customize-views-with-properties)&#x20;
  {% endtab %}

{% tab title="🔴 主題" %}

* [local computed variables](/ios/swift/type/prop/computed-properties/local-computed-variables.md)
* [mutating getter / nonmutating setter](/ios/swift/type/prop/computed-properties/mutating-getter-and-nonmutating-setter.md)
  {% endtab %}

{% tab title="👥 相關" %}

* [property observer (willSet/didSet)](/ios/swift/type/prop/stored-property/observer.md)： you can add in *<mark style="color:purple;">computed properties</mark>* that you [inherit](/ios/swift/type/inheritance.md).
  {% endtab %}

{% tab title="📘 手冊" %}

* Swift ⟩ [Properties](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/properties/) ⟩&#x20;
  * [Stored Properties](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/properties/#Stored-Properties) &#x20;
  * [Computed Properties](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/properties/#Computed-Properties)
    {% endtab %}
    {% endtabs %}
