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.
Asked
Active
Viewed 2,435 times
4

Chen Li Yong
- 5,459
- 8
- 58
- 124
-
1I believe that Any can refer to any type, including, for example, a closure/function. AnyObject needs to be an instance of a class. – francisaugusto Feb 06 '18 at 07:15
-
@francisaugusto but I mean, why there's `AnyObject` if it can be handled by `Any`? I think there must be something that `AnyObject` can do, that `Any` can't. – Chen Li Yong Feb 06 '18 at 07:18
-
1please check here: https://stackoverflow.com/a/25809377/2450755 – mugx Feb 06 '18 at 07:19
-
1@AndreaMugnaini ooh okay, the last SO link really answers my question. Thanks! – Chen Li Yong Feb 06 '18 at 07:20
1 Answers
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