Alexander Alexander - 1 year ago 82
Swift Question

Type Casting For AnyObject to represent instances of any reference type

var array = [AnyObject]()
struct Test {}

When I write this code in the play ground it gives me the following error Type 'Test' does not conform to protocol 'AnyObject'

I am guessing it is failing because struct is a value type not a reference type.
But when I run this code

var array = [AnyObject]()

It works
but these are all also value types but in this case no error is given Why?

Answer Source

For your success case, there is some conversion going on behind the scenes.

Try adding this at the end of your appends to see what's happening:

for item in array {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download