ios 声明 NSDictionary 并将其放在 Swift 中的键值对?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26023509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Declaring NSDictionary and put it key value pair in Swift?
提问by serhanaksut
I have been trying to declare an NSDictionary with class type key and value like this :
我一直在尝试使用类类型键和值来声明一个 NSDictionary,如下所示:
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 ?
这里,“Category”和“SubCategory”是全局类。我知道我不能对关键字段使用类类型。但是,无论如何我都应该做到这一点。有没有办法做到这一点?我如何声明专门的 NSDictionary 或类似的东西来做到这一点?
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
此致
回答by Antonio
NSDictionary
does notsupport generics, so you cannot declare that way - if you want to use it, instantiate it as follows:
NSDictionary
它不支持泛型,所以你不能声明的方式-如果你想使用它,它实例如下:
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:
雨燕提供了一个新的字典类型,它不支持泛型,并建立它,你只需要更换NSDictionary
与Dictionary
您的代码:
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 canuse a class as key - what you need to do is make the key class (Category
in your code) implement the Hashable
and Equatable
protocols
最后,使用 swift 字典,您可以使用类作为关键 - 您需要做的是使关键类(Category
在您的代码中)实现Hashable
和Equatable
协议