[swift-users] Why does casting to Any make my forced downcast not crash?

Kim Burgestrand kim at burgestrand.se
Wed Mar 15 06:47:46 CDT 2017


On Tue, 14 Mar 2017 at 19:50 Joe Groff <jgroff at apple.com> wrote:

>
> It shouldn't. Please file a bug, if you haven't already.
>

Will do! I'll file a bug that casting to `Any` first causes different
behavior.

Follow-up question, looking at the expression `name as! T`, is it expected
to cause a nil-unwrapping runtime error when `name == nil` and `T ==
Optional<String>`? Mind you, `(nil as! Optional<String>)` does not cause an
error.

If the nil-unwrapping error is expected, how can I write it so that it
doesn't, and instead returns nil?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170315/440bd878/attachment.html>


More information about the swift-users mailing list