Casting Operators
Static casting operator (as
)
The static casting operator as
can be used to statically type cast a value to a type.
If the static type of the value is a subtype of the given type (the target type), the operator returns the value as the given type.
The cast is performed statically (i.e., when the program is type-checked). Only the static type of the value is considered — not the run-time type of the value.
This means it is not possible to downcast using this operator. Consider using the conditional downcasting operator as?
instead.
_22// Declare a constant named `integer` which has type `Int`._22//_22let integer: Int = 1_22_22// Statically cast the value of `integer` to the supertype `Number`._22// The cast succeeds, because the type of the variable `integer`,_22// the type `Int`, is a subtype of type `Number`._22// This is an upcast._22//_22let number = integer as Number_22// `number` is `1` and has type `Number`_22_22// Declare a constant named `something` which has type `AnyStruct`,_22// with an initial value which has type `Int`._22//_22let something: AnyStruct = 1_22_22// Statically cast the value of `something` to `Int`._22// This is invalid, the cast fails, because the static type of the value is type `AnyStruct`,_22// which is not a subtype of type `Int`._22//_22let result = something as Int
Conditional downcasting operator (as?
)
The conditional downcasting operator as?
can be used to dynamically type cast a value to a type. The operator returns an optional. If the value has a run-time type that is a subtype of the target type the operator returns the value as the target type; otherwise, the result is nil
.
The cast is performed at run-time (when the program is executed) and not statically (when the program is checked).
_17// Declare a constant named `something` which has type `AnyStruct`,_17// with an initial value which has type `Int`._17//_17let something: AnyStruct = 1_17_17// Conditionally downcast the value of `something` to `Int`._17// The cast succeeds, because the value has type `Int`._17//_17let number = something as? Int_17// `number` is `1` and has type `Int?`_17_17// Conditionally downcast the value of `something` to `Bool`._17// The cast fails, because the value has type `Int`,_17// and `Bool` is not a subtype of `Int`._17//_17let boolean = something as? Bool_17// `boolean` is `nil` and has type `Bool?`
Downcasting works for concrete types, but also works for nested types (e.g., arrays), interfaces, optionals, and so on.
_10// Declare a constant named `values` which has type `[AnyStruct]`,_10// i.e. an array of arbitrarily typed values._10//_10let values: [AnyStruct] = [1, true]_10_10let first = values[0] as? Int_10// `first` is `1` and has type `Int?`_10_10let second = values[1] as? Bool_10// `second` is `true` and has type `Bool?`
Force-downcasting operator (as!
)
The force-downcasting operator as!
behaves like the conditional downcasting operator as?
. However, if the cast succeeds, it returns a value of the given type instead of an optional; if the cast fails, it aborts the program instead of returning nil
:
_17// Declare a constant named `something` which has type `AnyStruct`,_17// with an initial value which has type `Int`._17//_17let something: AnyStruct = 1_17_17// Force-downcast the value of `something` to `Int`._17// The cast succeeds, because the value has type `Int`._17//_17let number = something as! Int_17// `number` is `1` and has type `Int`_17_17// Force-downcast the value of `something` to `Bool`._17// The cast fails, because the value has type `Int`,_17// and `Bool` is not a subtype of `Int`._17//_17let boolean = something as! Bool_17// Run-time error