初始化器
init(clamping:)
使用最接近給定整數的可表示值創建一個新實例。
聲明
init<Other>(clamping source: Other) where Other : BinaryInteger
參數
source
要轉換為此類型的整數。
詳述
如果作為 source
傳遞的值大於此類型中的最大可表示值,則結果是該類型的 max
值。如果 source
小於此類型中的最小可表示值,則結果是該類型的 min
值。
在此示例中,通過將 500
限製在 -128...127
範圍內,將 x
初始化為 Int8
實例,通過將 -500
限製在 0...UInt.max
範圍內,將 y
初始化為 UInt
實例.
let x = Int8(clamping: 500)
// x == 127
// x == Int8.max
let y = UInt(clamping: -500)
// y == 0
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+
相關用法
- Swift Int16 init(_:radix:)用法及代碼示例
- Swift Int16 init(truncatingIfNeeded:)用法及代碼示例
- Swift Int16 init(_:)用法及代碼示例
- Swift Int16 init(exactly:)用法及代碼示例
- Swift Int16 init(integerLiteral:)用法及代碼示例
- Swift Int16 ..<(_:)用法及代碼示例
- Swift Int16 random(in:using:)用法及代碼示例
- Swift Int16 ^=(_:_:)用法及代碼示例
- Swift Int16 +(_:)用法及代碼示例
- Swift Int16 magnitude用法及代碼示例
- Swift Int16 %(_:_:)用法及代碼示例
- Swift Int16 /=(_:_:)用法及代碼示例
- Swift Int16 <<=(_:_:)用法及代碼示例
- Swift Int16 <<(_:_:)用法及代碼示例
- Swift Int16 random(in:)用法及代碼示例
- Swift Int16 ...(_:)用法及代碼示例
- Swift Int16 >>(_:_:)用法及代碼示例
- Swift Int16 *(_:_:)用法及代碼示例
- Swift Int16 &-=(_:_:)用法及代碼示例
- Swift Int16 quotientAndRemainder(dividingBy:)用法及代碼示例
- Swift Int16 |(_:_:)用法及代碼示例
- Swift Int16 -(_:_:)用法及代碼示例
- Swift Int16 ^(_:_:)用法及代碼示例
- Swift Int16 &*(_:_:)用法及代碼示例
- Swift Int16 &<<(_:_:)用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 Int16 init(clamping:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。