Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring NSDictionary and put it key value pair in Swift?

Tags:

ios

swift

iphone

I have been trying to declare an NSDictionary with class type key and value like this :

var catAndSubCatDict: NSDictionary<Category, Array<SubCategory>> = NSDictionary<Category, Array<SubCategory>>()

Here, "Category" and "SubCategory" are the global classes. I know that I can not use a class type for key field. However, I should achieve this anyway. Is there any way to do this ? How can I declare specialized NSDictionary or similar things to do this ?

In addition, I use this dictionary like this :

for(var i=0; i<resultJson.count; i++)
{
   let subCategoryItemList = resultJson.objectAtIndex(i) as NSDictionary
   let categoryItem = subCategoryItemList.valueForKey(WSConstants.CATEGORY_OBJ) as NSDictionary

   var category: Category = Category()
   category.categoryId    = categoryItem.valueForKey(WSConstants.CATEGORY_ID) as Int
   category.categoryName  = categoryItem.valueForKey(WSConstants.NAME) as String

   var subCategoryList: Array<SubCategory> = []
   let jsonSubCategoryList = subCategoryItemList.allKeysForObject(WSConstants.SUBCATEGORY_LIST_OBJ) as NSArray

   for(var i=0; i<jsonSubCategoryList.count; i++)
   {
      let subCategoryObj = jsonSubCategoryList.objectAtIndex(i) as NSDictionary

      var subCategory: SubCategory = SubCategory()
      subCategory.subCategoryId    = subCategoryObj.valueForKey(WSConstants.SUBCATEGORY_ID) as Int
      subCategory.subCategoryName  = subCategoryObj.valueForKey(WSConstants.NAME) as String
      subCategory.subCategoryType  = subCategoryObj.valueForKey(WSConstants.SUBCATEGORY_TYPE) as Int

      subCategoryList.append(subCategory)
   }
   self.catAndSubCatDict.setValue(category, forkey: subCategoryList) // Also, I have to achieve this
}

Thank you for your answers,

Best regards

like image 829
serhanaksut Avatar asked Sep 24 '14 18:09

serhanaksut


1 Answers

NSDictionary does not support generics, so you cannot declare that way - if you want to use it, instantiate it as follows:

var catAndSubCatDict: NSDictionary = NSDictionary()

Swift provides a new dictionary type, which does support generics, and to create it you just have 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 cases mentioned above you can use type inference and shorten the code by removing the variable type:

var catAndSubCatDict = NSDictionary()
var catAndSubCatDict = Dictionary<Category, Array<SubCategory>>()
var catAndSubCatDict = [Category : Array<SubCategory>]()
var catAndSubCatDict = [Category : [SubCategory]]()

Last, with swift dictionaries you can use a class as key - what you need to do is make the key class (Category in your code) implement the Hashable and Equatable protocols

like image 58
Antonio Avatar answered Nov 15 '22 08:11

Antonio