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
Swift: Get the Number of Keys In a Dictionary
提问by blee908
For swift arrays, we can simply use the count
property 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.count
will work.
字典上有一个 count 属性,所以myDict.count
会起作用。
回答by Mike
Why not simply use the count
property on myDict
?
为什么不简单地使用该count
属性myDict
?
myDict.count