Returns the bits of the given instance, interpreted as having the specified type.
func unsafeBitCast<T, U>(_ x: T, to type: U.Type) -> U
The instance to cast to
The type to cast
typeand the type of
xmust have the same size of memory representation and compatible memory layout.
A new instance of type
U, cast from
Use this function only to convert the instance passed as
x to a layout-compatible type when conversion through other means is not possible. Common conversions supported by the Swift standard library include the following:
Value conversion from one integer type to another. Use the destination type’s initializer or the
Bitwise conversion from one integer type to another. Use the destination type’s
Conversion from a pointer to an integer value with the bit pattern of the pointer’s address in memory, or vice versa. Use the
init(bitPattern:)initializer for the destination type.
Casting an instance of a reference type. Use the casting operators (
as?) or the
unsafeDowncast(_:to:)function. Do not use
unsafeBitCast(_:to:)with class or pointer types; doing so may introduce undefined behavior.