They are similar but they are not the same.
What as
does is to let the compiler know that you want to use an object as another.
In your example, tableView.dequeueReusableCell
returns UITableViewCell?
, which is an optional that can contain either nothing or UITableViewCell
.
Optionals can be unwrapped in a few ways, either with if/let
:
if let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") {
// cell is a UITableViewCell here, not an optional
}
// cell doesn't exist here anymore
Using a guard
guard let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") else {
// cell is nil so it's not even created. We HAVE to exit here
return
}
// cell is a cell is UITableView here, no optional
Or by forcing it, which is not safe and can cause your app to crash. (Personally I avoid these as much as I can, and only use them with IBOutlets
)
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")!
// cell will be UITableViewCell!, which is an optional that will
// crash if you try to use it and it's nil
Now back to your question, you have a UITableViewCell?
, which is a regular optional.
In your first example, you are force-unwrapping it first with !
, and then casting it to UITableViewCell
. This will result in a UITableViewCell
that is actually a force-unwrapped-optional, which will crash your app if it's nil and you try to use it.
In your second example, you are telling the compiler you want to use UITableViewCell?
as UITableViewCell!
, which will result in the same scenario.
In neither example you need to use ?
or !
when using cell
, since it's already force-unwrapped.
I would, however, suggest you use a guard to avoid using force-unwrapped-optionals. This is how I handle custom cells:
guard let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? CustomCell else {
fatalError("CustomCell is not in the table view")
// Crash with a custom message instead of a generic one
}
Note that I'm using tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
, this method doesn't return an optional, so if you want to use a cell that doesn't have a custom class, you can use this and don't have to worry about unwrapping.