Categories
ios 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. 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

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