初始化器
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 Int32 init(integerLiteral:)用法及代碼示例
- Swift Int32 init(truncatingIfNeeded:)用法及代碼示例
- Swift Int32 init(_:radix:)用法及代碼示例
- Swift Int32 init(_:)用法及代碼示例
- Swift Int32 init(exactly:)用法及代碼示例
- Swift Int32 <<=(_:_:)用法及代碼示例
- Swift Int32 &=(_:_:)用法及代碼示例
- Swift Int32 &<<=(_:_:)用法及代碼示例
- Swift Int32 ...(_:_:)用法及代碼示例
- Swift Int32 negate()用法及代碼示例
- Swift Int32 multipliedFullWidth(by:)用法及代碼示例
- Swift Int32 &(_:_:)用法及代碼示例
- Swift Int32 +=(_:_:)用法及代碼示例
- Swift Int32 &+(_:_:)用法及代碼示例
- Swift Int32 !=(_:_:)用法及代碼示例
- Swift Int32 -(_:)用法及代碼示例
- Swift Int32 ..<(_:_:)用法及代碼示例
- Swift Int32 -=(_:_:)用法及代碼示例
- Swift Int32 %(_:_:)用法及代碼示例
- Swift Int32 <<(_:_:)用法及代碼示例
- Swift Int32 trailingZeroBitCount用法及代碼示例
- Swift Int32 >>(_:_:)用法及代碼示例
- Swift Int32 &>>(_:_:)用法及代碼示例
- Swift Int32 -(_:_:)用法及代碼示例
- Swift Int32 &>>=(_:_:)用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 Int32 init(clamping:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。