Generic Function

unsafeDowncast(_:to:)

Returns the given instance cast unconditionally to the specified type.

Declaration

func unsafeDowncast<T>(_ x: AnyObject, to type: T.Type) -> T where T : AnyObject

Parameters

x

An instance to cast to type T.

type

The type T to which x is cast.

Return Value

The instance x, cast to type T.

Discussion

The instance passed as x must be an instance of type T.

Use this function instead of unsafeBitcast(_:to:) because this function is more restrictive and still performs a check in debug builds. In -O builds, no test is performed to ensure that x actually has the dynamic type T.

See Also

Instance Casting

func unsafeBitCast<T, U>(T, to: U.Type) -> U

Returns the bits of the given instance, interpreted as having the specified type.