16

What's the difference between these two code snippets:

let cell = tableView.dequeueReusableCellWithIdentifier("cellId") as UITableViewCell?
// vs
let cell = tableView.dequeueReusableCellWithIdentifier("cellId") as? UITableViewCell

Isn't the result exactly the same?

cfischer
  • 24,452
  • 37
  • 131
  • 214
  • `as UITableViewCell?` results in compile error in Swift 2 `AnyObject? is not convertible to UITableViewCell?` – onmyway133 Sep 23 '15 at 16:12

3 Answers3

14

In that code there's no difference, in both cases it evaluates to UITableViewCell?

The real difference is:

  • in the first case a downcast to UITableViewCell? is expected to always succeed (even if it's nil), so if dequeueReusableCellWithIdentifier returns something that's not an instance of UITableViewCell (or an instance of a class inherited from it), it fails at runtime. The expression returns an optional UITableViewCell?

  • in the second case the cast is optional: if the object returned by dequeueReusableCellWithIdentifier is neither an instance of UITableViewCell nor an instance of a subclass, the downcast gracefully evaluates to nil (hence with no runtime error).

Of course dequeueReusableCellWithIdentifier always returns a UITableViewCell, that's why there's no difference in your code. But in other contexts the difference may exist and you have to take care of that to prevent runtime errors

Antonio
  • 71,651
  • 11
  • 148
  • 165
7

Main difference between as and as? is that as is forced cast and will crash if unsuccessful. as? will return optional value containing value if cast was successful and nil if unsuccessful.

Kirsteins
  • 27,065
  • 8
  • 76
  • 78
  • 1
    Does this mean that `as` and `as!` are the same? – BR41N-FCK Nov 04 '15 at 13:06
  • @BR41N-FCK This has changed a bit, in Swift 2.0 `as` is only valid if there is no chance of failure (you will get compile time error if you use it when not allowed). `as!` is called forced casting and will crash if cast is not possible at runtime. – Kirsteins Nov 04 '15 at 13:10
  • Is there a comparison of the three to get a better grasp of the nuances between them? – BR41N-FCK Nov 04 '15 at 16:21
  • @BR41N-FCK `as` - available only if cast cannot fail. `as?` - optional cast (result is nil if cast is not possible), result is optional type. `as!` - forced cast, crash if cast is not possible. – Kirsteins Nov 04 '15 at 18:25
  • If the case is such as what you've just described, is there any point for `as!` to exist? I mean if the cast can fail and you can deal with failure and go on `as?` seems the thing to go for, and if you can't allow any failure and it has to work then `as` seems applicable. And so why use `as!` especially considering the fact it can cause a run-time error and piss off the user. – BR41N-FCK Nov 09 '15 at 10:19
2

The difference between, as and as?

  • as? UITableViewCell means when you don't know what you're downcasting, you are assuming that as a UITableViewCell, but it might me Integer or Float or Array or Dictionary.

  • as UITableViewCell? means it's an Optional Value, it may either contain a UITableViewCell or Nil value.

arthankamal
  • 6,341
  • 4
  • 36
  • 51