イニシャライザ


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 ビットのみが使用されます。


  1. let p: Int16 = -500
  2. // 'p' has a binary representation of 11111110_00001100
  3. let q = Int8(truncatingIfNeeded: p)
  4. // q == 12
  5. // 'q' has a binary representation of 00001100

T のビット幅がこの型のビット幅より小さい場合、残りのビットを埋めるために結果は 符号拡張 されます。つまり、source が負の場合、結果は 1 で埋められ、そうでない場合は 0 で埋められます。


  1. let u: Int8 = 21
  2. // 'u' has a binary representation of 00010101
  3. let v = Int16(truncatingIfNeeded: u)
  4. // v == 21
  5. // 'v' has a binary representation of 00000000_00010101
  6. let w: Int8 = -21
  7. // 'w' has a binary representation of 11101011
  8. let x = Int16(truncatingIfNeeded: w)
  9. // x == -21
  10. // 'x' has a binary representation of 11111111_11101011
  11. let y = UInt16(truncatingIfNeeded: w)
  12. // y == 65515
  13. // 'y' has a binary representation of 11111111_11101011













トップへ












トップへ












トップへ












トップへ












トップへ












トップへ












トップへ












トップへ












トップへ












トップへ












トップへ












トップへ












トップへ












トップへ












トップへ












トップへ












トップへ












トップへ