2017-10-12 202 views
-5

在Objective-C有可能那样做:Swift如何获取字典中键值的数量?

NSString *sectionTitle = [animalSectionTitles objectAtIndex:section]; 
NSArray *sectionAnimals = [animals objectForKey:sectionTitle]; 
return [sectionAnimals count]; 

如果动物是[字符串:字符串]字典 但是在斯威夫特3没有对字典的方法objectForKey(String:)或任何类似的或者我可以“找不到它

+1

使用'动物[sectionTitle]' – Venkat

+1

你试过'动物[sectionTitle]'? – TawaNicolas

+1

请阅读:[Swift Language Guide:Swift Tour](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/GuidedTour.html#//apple_ref/doc/uid/TP40014097- CH2-ID1)。即使在Objective-C中,您也可以(也应该)使用密钥和索引订阅。 – vadian

回答

4

在迅速的,你可以做到这一点

let sectionAnimals:Array = animals[sectionTitle] 

例如

let animals:[String:String] = ["key" : "value"] 
    let sectionTitle = animalSectionTitles[0] 
    let sectionAnimals = animals["key"] 

如需更多帮助,您可以按照本Tutorial Link