Denis Windover
Denis Windover

Reputation: 445

Swift what is correct syntax of dictionary?

 var myCountry:String?
var myCity:String?
var myBodyType:String?
var myOrientation:String?
var myRelationship:String?

I need to build a dictionary variable from this data but the myCountry and myCity I want to combine under the one key "region". I tryed:

var myData = ["region":["country":myCountry,"city":myCity],"bodyType":myBodyType,"orientation":myOrientation,"relationship":myRelationship]

its wrong. how to do this?

Upvotes: 3

Views: 1714

Answers (1)

vadian
vadian

Reputation: 285250

Basically the syntax is correct, but ...

...in Swift 3 you get this error:

heterogenous collection literal could only be inferred to '[String : Any]'; add explicit type annotation if this is intentional

That means you have to write

var myData : [String:Any] = ["region" ...

Consider also to handle the optionals because in Swift a nil value means the key is missing.

Upvotes: 12

Related Questions