简体   繁体   中英

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. However, we can't do the same for dictionary keys. Is the only way to do this to use a for loop and counter?

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

在此处输入图片说明

Swift 4

Dictionary has a inbuilt count property.

For the number of elements in myDict Dictionary, you can call

myDict.count

For further reference you can check the documentation at Apple Documents in the below links,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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