用法一
初始化器
init(_:)
從給定的浮點值創建一個整數,向零舍入。
必需的。提供的默認實現。
聲明
init<T>(_ source: T) where T : BinaryFloatingPoint
參數
source
要轉換為整數的浮點值。
source
在向零舍入後必須可以在此類型中表示。
詳述
作為 source
傳遞的值的任何小數部分都將被刪除,將值四舍五入到零。
let x = Int(21.5)
// x == 21
let y = Int(-21.5)
// y == -21
如果 source
在向零舍入後超出此類型的範圍,則可能會發生運行時錯誤。
let z = UInt(-21.5)
// Error: ...the result would be less than UInt.min
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+
用法二
初始化器
init(_:)
從給定的整數創建一個新實例。
必需的。提供的默認實現。
聲明
init<T>(_ source: T) where T : BinaryInteger
參數
source
要轉換的整數。
source
必須在此類型中可表示。
詳述
如果作為 source
傳遞的值在此類型中不可表示,則可能會發生運行時錯誤。
let x = -500 as Int
let y = Int32(x)
// y == -500
// -500 is not representable as a 'UInt32' instance
let z = UInt32(x)
// Error
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+
相關用法
- Swift BinaryInteger init(clamping:)用法及代碼示例
- Swift BinaryInteger init(truncatingIfNeeded:)用法及代碼示例
- Swift BinaryInteger init(exactly:)用法及代碼示例
- Swift BinaryInteger quotientAndRemainder(dividingBy:)用法及代碼示例
- Swift BinaryInteger trailingZeroBitCount用法及代碼示例
- Swift BinaryInteger <<=(_:_:)用法及代碼示例
- Swift BinaryInteger !=(_:_:)用法及代碼示例
- Swift BinaryInteger /(_:_:)用法及代碼示例
- Swift BinaryInteger ==(_:_:)用法及代碼示例
- Swift BinaryInteger %=(_:_:)用法及代碼示例
- Swift BinaryInteger &=(_:_:)用法及代碼示例
- Swift BinaryInteger |(_:_:)用法及代碼示例
- Swift BinaryInteger +(_:_:)用法及代碼示例
- Swift BinaryInteger &(_:_:)用法及代碼示例
- Swift BinaryInteger ^=(_:_:)用法及代碼示例
- Swift BinaryInteger ^(_:_:)用法及代碼示例
- Swift BinaryInteger %(_:_:)用法及代碼示例
- Swift BinaryInteger *(_:_:)用法及代碼示例
- Swift BinaryInteger -(_:_:)用法及代碼示例
- Swift BinaryInteger /=(_:_:)用法及代碼示例
- Swift BinaryInteger >>=(_:_:)用法及代碼示例
- Swift BinaryInteger ~(_:)用法及代碼示例
- Swift BinaryInteger |=(_:_:)用法及代碼示例
- Swift BinaryInteger <<(_:_:)用法及代碼示例
- Swift BinaryInteger >>(_:_:)用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 BinaryInteger init(_:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。