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

Joe Groff jgroff at apple.com
Wed Mar 15 13:09:43 CDT 2017


> On Mar 15, 2017, at 4:47 AM, Kim Burgestrand <kim at burgestrand.se> wrote:
> 
> On Tue, 14 Mar 2017 at 19:50 Joe Groff <jgroff at apple.com <mailto: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 you're casting to an Optional destination type, then the cast ought to succeed and produce a `nil` result if the input is nil.

-Joe

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170315/986cdeda/attachment.html>


More information about the swift-users mailing list