ios Swift:获取字典中的键数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25633732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:10:50  来源:igfitidea点击:

Swift: Get the Number of Keys In a Dictionary

iosswift

提问by blee908

For swift arrays, we can simply use the countproperty to find out how many elements are in the collection. However, we can't do the same for dictionary keys. Is the only way to do this to use a for loop and counter?

对于 swift 数组,我们可以简单地使用该count属性来找出集合中有多少元素。但是,我们不能对字典键做同样的事情。这样做的唯一方法是使用 for 循环和计数器吗?

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

myDict.keys.count // doesn't work

回答by Shan

There is a count property on dictionary, so myDict.countwill work.

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

回答by Mike

Why not simply use the countproperty on myDict?

为什么不简单地使用该count属性myDict

myDict.count

enter image description here

在此处输入图片说明