イニシャライザ
init(truncatingIfNeeded:)
この型に適合するように符号拡張または切り捨てを行って、与えられたインスタンスのビットパターンから新しいインスタンスを作成します。
iOS 8.0+ iPadOS 8.0+ Mac Catalyst 13.0+ macOS 10.10+
tvOS 9.0+ visionOS 1.0+ watchOS 2.0+
init<T>(truncatingIfNeeded source: Other) where T : BinaryInteger
パラメータ
source
この型に変換すべき整数。
議論
T(source の型)のビット幅がこの型のビット幅以上の場合、結果は source の最下位ビットを切り捨てた値になります。例えば、16 ビット値を 8 ビット型に変換する場合、source の下位 8 ビットのみが使用されます。
- let p: Int16 = -500
- // 'p' has a binary representation of 11111110_00001100
- let q = Int8(truncatingIfNeeded: p)
- // q == 12
- // 'q' has a binary representation of 00001100
T のビット幅がこの型のビット幅より小さい場合、残りのビットを埋めるために結果は 符号拡張 されます。つまり、source が負の場合、結果は 1 で埋められ、そうでない場合は 0 で埋められます。
- let u: Int8 = 21
- // 'u' has a binary representation of 00010101
- let v = Int16(truncatingIfNeeded: u)
- // v == 21
- // 'v' has a binary representation of 00000000_00010101
- let w: Int8 = -21
- // 'w' has a binary representation of 11101011
- let x = Int16(truncatingIfNeeded: w)
- // x == -21
- // 'x' has a binary representation of 11111111_11101011
- let y = UInt16(truncatingIfNeeded: w)
- // y == 65515
- // 'y' has a binary representation of 11111111_11101011
トップへ
トップへ
トップへ
トップへ
トップへ
トップへ
トップへ
トップへ
トップへ
トップへ
トップへ
トップへ
トップへ
トップへ
トップへ
トップへ
トップへ
トップへ