How to assign a value to [any]
Asked Answered
D

2

-2

When I set c to a

var a: [Any]
var c: Array<PostCategory>

error shown:

cannot convert value of type 'Array' to expected argument type [Any]

how to solve the problem?

Derbyshire answered 12/5, 2016 at 12:29 Comment(2)
Where's the actual assignment taking place?Bobolink
Possible duplicate of Why aren't [SomeStruct] convertible to [Any]?Selfesteem
T
0

The error message is a bit misleading but try initializing the array before assigning it:

var c: Array<PostCategory> = []

...or...

var c = Array<PostCategory>()
Telegraphone answered 12/5, 2016 at 12:44 Comment(0)
I
0

I bet your PostCategory is a struct. Apparently struct arrays aren't convertible to an Any array. This is weird because all types conforms to the Any protocol.

If you change the PostCategory to a class instead, it should work fine. You might need to create a new initializer for the class though, since classes doesn't give you the same default initializer as a struct does.

Iridotomy answered 12/5, 2016 at 12:55 Comment(1)
Yes, PostCategory is a struct, Is there anything equivalent of Any for structDerbyshire

© 2022 - 2024 — McMap. All rights reserved.