---
description: 將變數限制在指定的範圍 (ClosedRange) 裡面。
---

# Clamped

{% tabs %}
{% tab title="@Clamped" %}
```swift
@propertyWrapper
public struct Clamped<T: Comparable> {
    
    private var _value: T
    private let _range: ClosedRange<T>
    
    public init(wrappedValue: T, in range: ClosedRange<T>) {
        self._value = wrappedValue.clamped(in: range)
        self._range = range
    }
    
    public var wrappedValue: T {
        get { _value }
        set { _value = newValue.clamped(in: _range) }
    }
}
```
{% endtab %}

{% tab title="⬆️ 需要" %}
* [.clamped-in](../../category/protocol/comparable/.clamped-in "mention")
{% endtab %}

{% tab title="📘 手冊" %}
* Swift  ⟩  Standard Lib  ⟩ &#x20;
  * [Collections](https://developer.apple.com/documentation/swift/swift_standard_library/collections)  ⟩ &#x20;
    * [ClosedRange](https://developer.apple.com/documentation/swift/closedrange)&#x20;
  * [Basic Behaviors](https://developer.apple.com/documentation/swift/swift_standard_library/basic_behaviors)  ⟩ &#x20;
    * [Comparable](https://developer.apple.com/documentation/swift/comparable)&#x20;
{% endtab %}

{% tab title="👥  相關" %}
* is a [](<> "mention").
* similar to [unitinterval](unitinterval "mention").
{% endtab %}
{% endtabs %}

