** Cast
** refers to treating variables and constants as more specific and general-purpose types.
For example, it means to treat a variable that was Any type
as String type
, and conversely to treat String type
as Any type
.
There are two types of cast, upcast
and downcast
.
The difference between upcast and downcast is whether you treat it as a more general type or a concrete type.
Use upcast
to treat it as a more general value (wider group).
Use as
when dealing with upcasts. Now let's write the code.
.swift
let string: String = "sample" //String type
let any: Any = string as Any //Upcast from String type to Any type
In this way ** value you want to upcast as type
** to upcast
You can upcast by writing in the format.
Downcast
refers to treating it as a more specific type.
This is not always the case with downcasts. Includes the possibility of failure.
So let's understand that the grammar is also premised on including failures.
There are two ways to write a downcast, using as? Or as!
.
Now let's write the code.
The way to write using as?
is as follows.
.swift
let any: Any = "sample" as Any
let string = any as? String //Optional("sample")
let int = any as? Int //nil
The third line of the above example tries to downcast to an Int type, but fails because "sample" cannot be an Int type. In that case the value is nil.
The feature of as? Is that it becomes nil when it fails in this way. Along with that, if you look at the second line, this is a successful downcast to the String type, but the value is an optional type of the String type. This can fail, so the result is optional. (Optional type means to allow nil in a nutshell)
Downcasting using as? In this way will result in nil if the result is optional and fails.
① The result will be an optional type
② If it fails, the result will be nil.
The downcast using as!
is written as follows.
.swift
let any: Any = "sample" as Any
let string = any as! String //"sample"
let int = any as! Int //Run-time error
The writing method itself is not so different from the case of as ?, but the result is different.
If the second line is as ?, the optional type is now the String type. In addition, there is a run-time error on the third line.
For those who know the optional type, it may have been roughly considered, but when using as !, the result is forcibly unwrapped, and instead of the result not being an optional type, at runtime if it fails. An error will occur and the app will crash. ** Downcasting using as! Is called forced casting. ** **
① The result is not optional
(2) If it fails, a run-time error will occur.
I've been doing a downcast method using as?
And as!
.
Instead of being safe, you have to write what to do if it fails. On the other hand, as! Risks an error at runtime instead of having to write what to do if it fails.
It can be said that both have advantages and disadvantages, but personally, I think it is better to use as ?, which is basically safe.
As a dangerous pattern, I tried pressing [Fix]
and it was solved, so it's okay! It is a pattern that causes a run-time error when executed. If you've read this far, you'll probably understand why it's not good, but be aware that using as! In this way tends to result in unstable code.
I would like to introduce the articles that I referred to when posting this article. https://fukatsu.tech/swift-cast
This time, I summarized about type cast
.
Typecasting is a basic part of swift, but I think it is necessary to understand it clearly because it is easy to encounter unexpected errors if you handle it without understanding it, or to fall into the state of writing it for the time being.
So, this time, in order to deepen my own learning, I organized and posted it while referring to other articles. Please refer to it!