4

I usually use [String: AnyObject]. But I noticed that Alamofire uses [String: Any]. I read somewhere that Any is "superior" than AnyObject or "encompasses" AnyObject. But other than that, I don't know if there's any different between them. Is there any downside in the long run if I define dictionary as [String: Any] instead of [String: AnyObject]? Thanks.

Chen Li Yong
  • 5,459
  • 8
  • 58
  • 124

1 Answers1

2

Any allowed use to work with a mix of different types including function and non-class types such as Int, String, and Bool. According to the documentation, the elements in this array are Structs that are value types, so in theory

AnyObject shouldn’t work in these cases.

we would use AnyObject for Class types because they are a little more specific than Any. But again, the use of AnyObject is just an option.

more explaination you can find here. I hope all your doubts will clear after go through this link

https://medium.com/@mimicatcodes/any-vs-anyobject-in-swift-3-b1a8d3a02e00

Jaydeep Vyas
  • 4,411
  • 1
  • 18
  • 44
  • I see. So I think it will be better to use [String: Any] in this case, because it's more compatible with more types. I usually store String, Int, Double, Bool, and such in my dictionary, so the use of AnyObject is actually incorrect. Thanks! – Chen Li Yong Feb 06 '18 at 07:22
  • 1
    Use Any confidently in Swift – Jaydeep Vyas Feb 06 '18 at 07:23