[swift-users] Assigning [Int] to [Any] crashes Swift 2.2
Joe Groff
jgroff at apple.com
Thu Apr 14 12:28:12 CDT 2016
> On Apr 14, 2016, at 9:24 AM, Jens Alfke <jens at mooseyard.com> wrote:
>
>
>> On Apr 14, 2016, at 9:00 AM, Joe Groff <jgroff at apple.com> wrote:
>>
>> This is a known bug. Converting arrays of value type to arrays of protocol type is not supported—only class covariance is supported with containers—but the compiler fails to catch some cases.
>
> Thanks for the info!
>
> I found that if I use AnyObject instead of Any, it does work correctly. Is that because AnyObject isn’t a protocol? Or because conversion to AnyObject is a ‘magic’ case that uses Obj-C bridging?
Yeah, it's magic. Int is bridged to NSNumber, so converting to an array of object type works via the bridge.
-Joe
>
> My intuition is that [Any] will be more efficient than [AnyObject] for primitive values like numbers, because the AnyObject conversion boxes the value into a heap object, while converting to Any doesn’t. Or is that incorrect?
>
> —Jens
More information about the swift-users
mailing list