2017-10-12 33 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