初始化器
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 Int init(exactly:)用法及代码示例
- Swift Int init(truncatingIfNeeded:)用法及代码示例
- Swift Int init(_:radix:)用法及代码示例
- Swift Int init(_:)用法及代码示例
- Swift Int init(integerLiteral:)用法及代码示例
- Swift Int >>=(_:_:)用法及代码示例
- Swift Int &>>=(_:_:)用法及代码示例
- Swift Int *=(_:_:)用法及代码示例
- Swift Int ^(_:_:)用法及代码示例
- Swift Int &-(_:_:)用法及代码示例
- Swift Int |(_:_:)用法及代码示例
- Swift Int &=(_:_:)用法及代码示例
- Swift Int <<(_:_:)用法及代码示例
- Swift Int +(_:)用法及代码示例
- Swift Int >>(_:_:)用法及代码示例
- Swift Int ..<(_:)用法及代码示例
- Swift Int /=(_:_:)用法及代码示例
- Swift Int multipliedFullWidth(by:)用法及代码示例
- Swift Int -(_:_:)用法及代码示例
- Swift Int ...(_:_:)用法及代码示例
- Swift Int &*=(_:_:)用法及代码示例
- Swift Int &(_:_:)用法及代码示例
- Swift Int /(_:_:)用法及代码示例
- Swift Int ...(_:)用法及代码示例
- Swift Int ..<(_:_:)用法及代码示例
注:本文由纯净天空筛选整理自apple.com大神的英文原创作品 Int init(clamping:)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。