【问题标题】:Swift How to get amount of values for key in dictionary?Swift 如何获取字典中键的值的数量?
【发布时间】:2018-03-24 07:21:51
【问题描述】:

在 Objective-C 中可以这样做:

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

其中 animals 是 [String: String] 字典 但是在 Swift 3 中没有方法 objectForKey(String: ) 用于字典或任何类似的方法,或者我找不到它

【问题讨论】:

  • 使用animals[sectionTitle]
  • 你试过animals[sectionTitle]吗?
  • 请阅读:Swift Language Guide: A Swift Tour。即使在 Objective-C 中,您也可以(并且应该)使用键和索引订阅。

标签: ios swift dictionary


【解决方案1】:

你可以很快做到这一点

   let sectionAnimals:Array =  animals[sectionTitle]

例如

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

如需更多帮助,您可以关注本教程Tutorial Link

【讨论】:

    猜你喜欢
    • 2014-11-02
    • 1970-01-01
    • 1970-01-01
    • 2016-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-27
    • 1970-01-01
    相关资源
    最近更新 更多