NSDictionary does not support generics, so you cannot declare this path - if you want to use it, create it like this:
var catAndSubCatDict: NSDictionary = NSDictionary()
Swift offers a new type of dictionary that supports generics, and to create it you just need to replace NSDictionary with Dictionary in your code:
var catAndSubCatDict: Dictionary<Category, Array<SubCategory>> = Dictionary<Category, Array<SubCategory>>()
or use one of these compact forms:
var catAndSubCatDict: [Category : Array<SubCategory>] = [Category : Array<SubCategory>]() var catAndSubCatDict: [Category : [SubCategory]] = [Category : [SubCategory]]()
Note that in all the cases mentioned above, you can use type inference and shorten the code by deleting the variable type:
var catAndSubCatDict = NSDictionary() var catAndSubCatDict = Dictionary<Category, Array<SubCategory>>() var catAndSubCatDict = [Category : Array<SubCategory>]() var catAndSubCatDict = [Category : [SubCategory]]()
Finally, with quick dictionaries you can use the class as a key - you need to make a key class ( Category in your code) to implement Hashable and Equatable protocols
source share