O'Reilly logo

Mastering Swift 4 - Fourth Edition by Jon Hoffman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Is the dictionary empty?

To test whether the dictionary contains any key-value pairs at all, we can use the isEmpty property. This property will return false if the dictionary contains one or more key-value pairs and true if it is empty. The following example shows how to use this property to determine whether our dictionary contains any key-value pairs:

let countries = ["US":"United States", "IN":"India","UK":"United Kingdom"]  
var empty = countries.isEmpty 

In the preceding code, the isEmpty property returned false as there are three key-value pairs in the countries dictionary.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required