简体   繁体   中英

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM