[swift-evolution] [Pitch] Detecting and working with Optionals from Any

Joe Groff jgroff at apple.com
Wed Jun 22 11:12:24 CDT 2016


> On Jun 21, 2016, at 10:11 PM, Charlie Monroe <charlie at charliemonroe.net> wrote:
> 
> Unfortunately, this is not as easy, because automatic bridging won't be applied:
> 
> let myString: String? = "Hello"
> let anyValue: Any = myString
> 
> myString as? AnyObject // _NSContiguousString
> anyValue as? AnyObject // nil, since String is struct
> 
> let array: [String]? = ["Hello"]
> let anyArray: Any = array
> anyArray as? AnyObject // nil
> anyArray as? [AnyObject] // nil
> array as? AnyObject // ["Hello"]
> 
> And this goes for strings, arrays, dictionaries and possibly other types. Which means that you need to handle manually all of the bridging to ObjC types, which has really grown in Swift 3, taking into account all the Foundation types that are now structs.

I'd call that a bug, not something requiring evolution discussion. We're trying to phase this behavior out of dynamic casts, though (see https://github.com/apple/swift-evolution/blob/master/proposals/0083-remove-bridging-from-dynamic-casts.md).

-Joe


More information about the swift-evolution mailing list