# Difference between "as? NSError" and "as NSError?"

**URL:** <https://forums.kodeco.com/t/difference-between-as-nserror-and-as-nserror/113378>\
**Category:** Core Data by Tutorials\
**Created:** [June 28, 2020, 9:20am UTC](https://forums.kodeco.com/t/difference-between-as-nserror-and-as-nserror/113378 "2020-06-28T09:20:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ritsz](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/ritsz/32/91236_2.png) [@ritsz](https://forums.kodeco.com/u/ritsz)\
**Post date:** [June 28, 2020, 9:20am UTC](https://forums.kodeco.com/t/difference-between-as-nserror-and-as-nserror/113378/1 "2020-06-28T09:20:43Z")

</div>

In some chapters of the book there’s lines like this:

`if let error = error as NSError?`

whereas in Swift I usually see optional binding like this:

`if let something = something as? Type`

Can someone explain the difference and why we’re using “as Type?” instead of “as? Type”, I don’t get it.

Thank you!

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [July 7, 2020, 4:14pm UTC](https://forums.kodeco.com/t/difference-between-as-nserror-and-as-nserror/113378/2 "2020-07-07T16:14:55Z")

</div>

@ritsz The first line of code uses `as` to typecast `error` to `NSError?` because you are sure that `error` is of type `NSError?` in this case.

The second line of code uses `as?` to downcast `something` to `Type` since this definitely fails if `something` isn’t of type `Type`.

Please do let me know if you have any other questions or issues about the whole thing when you get a chance. Thank you!

---

<div class="post-metadata">

**Author:** ![ritsz](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/ritsz/32/91236_2.png) [@ritsz](https://forums.kodeco.com/u/ritsz)\
**Post date:** [July 7, 2020, 4:53pm UTC](https://forums.kodeco.com/t/difference-between-as-nserror-and-as-nserror/113378/3 "2020-07-07T16:53:25Z")

</div>

To me it looks weird to use `if let` when typecasting to an optional, I’m used to see it for optional binding (to unwrap the optional) instead, but I understand. Thank you.

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [July 7, 2020, 5:20pm UTC](https://forums.kodeco.com/t/difference-between-as-nserror-and-as-nserror/113378/4 "2020-07-07T17:20:13Z")

</div>

@ritsz You use `if let` in the first case to unwrap `error` because it may be `nil` since it is of type `NSError?`.

You use `if let` in the second case to unwrap `something` because it is `nil` if the downcast to `Type` fails.

Please do let me know if you have any other questions or issues about the whole thing when you get a chance. Thank you!

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [December 12, 2020, 1:20am UTC](https://forums.kodeco.com/t/difference-between-as-nserror-and-as-nserror/113378/5 "2020-12-12T01:20:46Z")

</div>

Closed on December 12, 2020, 1:20am UTC

This topic was automatically closed after 166 days. New replies are no longer allowed.
