Generic Initializer

init(exactly:)

Creates a new instance from the given integer, if it can be represented exactly.

Declaration

init?<T>(exactly source: T) where T : BinaryInteger

Parameters

source

A value to convert to this type of integer.

Discussion

If the value passed as source is not representable exactly, the result is nil. In the following example, the constant x is successfully created from a value of 100, while the attempt to initialize the constant y from 1_000 fails because the Int8 type can represent 127 at maximum:

let x = Int8(exactly: 100)
// x == Optional(100)
let y = Int8(exactly: 1_000)
// y == nil

See Also

Converting Integers

init<T>(T)

Creates a new instance from the given integer.

init<Other>(clamping: Other)

Creates a new instance with the representable value that’s closest to the given integer.

init<T>(truncatingIfNeeded: T)

Creates a new instance from the bit pattern of the given instance by truncating or sign-extending if needed to fit this type.

init(bitPattern: UInt)

Creates a new instance with the same memory representation as the given value.