How do I put different types in a dictionary in the Swift Language? -
swift allows dictionary contain single type.
here's definition taken swift book:
a dictionary container stores multiple values of same type
[...]
they differ objective-c’s
nsdictionary
,nsmutabledictionary
classes, can use kind of object keys , values , not provide information nature of these objects.
if that’s case how going create nested dictionaries?
imagine have plist
holds string, array , dictionary items in . if i’m allowed hold same of type of items (either string, array etc.) how going use different types of items stored in plist?
how put different types in same dictionary in swift?
you can achieve plist-like nested structures using any
type dictionary values swift's counterpart objective-c's id
type can hold value types.
var response = dictionary<string, any>() response["user"] = ["login": "power ranger", "password": "mighty morfin'"] response["status"] = 200
edit:
any
seems better anyobject
because in above code response["status"]
of type swift.int
, while using value type of anyobject
__nscfnumber
.
Comments
Post a Comment