代码之家  ›  专栏  ›  技术社区  ›  blee908

Swift:获取字典中的键数

  •  44
  • blee908  · 技术社区  · 10 年前

    对于swift数组,我们可以简单地使用 count 属性以确定集合中有多少个元素。但是,我们不能对字典键执行相同的操作。唯一的方法是使用for循环和计数器吗?

    var myDict:  [String: AnyObject] = // ... intialize dictionary with keys and values
    
    myDict.keys.count // doesn't work
    
    3 回复  |  直到 10 年前
        1
  •  92
  •   Shan    8 年前

    字典上有count属性,因此 myDict.count 将起作用。

        2
  •  13
  •   Jeffrey Huang    8 年前

    为什么不简单地使用 count 上的属性 myDict ?

    myDict.count
    

    enter image description here

        3
  •  -2
  •   Vivek    3 年前

    斯威夫特4

    字典内置 count 所有物

    对于 myDict 字典,你可以调用

    myDict.count
    

    如需进一步参考,您可以在以下链接中查看Apple文档中的文档,

    https://developer.apple.com/documentation/swift/dictionary https://developer.apple.com/documentation/swift/dictionary/3017863-count