简体   繁体   English

Swift:获取字典中的键数

[英]Swift: Get the Number of Keys In a Dictionary

For swift arrays, we can simply use the count property to find out how many elements are in the collection.对于 swift 数组,我们可以简单地使用count属性来找出集合中有多少元素。 However, we can't do the same for dictionary keys.但是,我们不能对字典键做同样的事情。 Is the only way to do this to use a for loop and counter?这样做的唯一方法是使用 for 循环和计数器吗?

var myDict:  [String: AnyObject] = // ... intialize dictionary with keys and values

myDict.keys.count // doesn't work

字典上有一个 count 属性,所以myDict.count会起作用。

Why not simply use the count property on myDict ?为什么不简单地在myDict上使用count属性?

myDict.count

在此处输入图片说明

Swift 4斯威夫特 4

Dictionary has a inbuilt count property.字典有一个内置的count属性。

For the number of elements in myDict Dictionary, you can call对于myDict字典中的元素数量,您可以调用

myDict.count

For further reference you can check the documentation at Apple Documents in the below links,如需进一步参考,您可以通过以下链接查看 Apple 文档中的文档,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM