Max Max - 1 year ago 47
iOS Question

How to create Mixed Pattern Dictionaries in Swift

I am trying to create a mixed pattern dictionary in swift which contains either string:string or string:dict as below. can anyone help.

var tempDict = ["Electronics":"TV"],["Home":["Kitchen":"Utensils"],["BedRoom":"Bed "],["DiningRoom":"Dining"]]

Answer Source

I may be missing what you really want to do, but you can declare a dictionary like this:

var tempDict = ["Electronics":"TV","Home":["Kitchen":"Utensils"],"BedRoom":"Bed ","DiningRoom":"Dining"]

Swift (as of 2.2.1) infers the type of tempDict as [String: NSObject], so you can use the values with casting:

if let electronics = tempDict["Electronics"] as? String {
if let home = tempDict["Home"] as? [String: String] {