How to assign a value to [any]

吃可爱长大的小学妹 提交于 2019-12-13 22:30:30

问题


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?


回答1:


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

var c: Array<PostCategory> = []

...or...

var c = Array<PostCategory>()



回答2:


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.



来源:https://stackoverflow.com/questions/37186934/how-to-assign-a-value-to-any

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!