Swift .isEmpty

Christine_Yang's avatar
Published Oct 11, 2022
Contribute to Docs

The .isEmpty property will return a true value if there are no key-value pairs in a dictionary and false if the dictionary does contain key-value pairs.

  • Learn how to build iOS applications with Swift and SwiftUI and publish them to Apples' App Store.
    • Includes 7 Courses
    • With Certificate
    • Beginner Friendly.
      13 hours
  • A powerful programming language developed by Apple for iOS, macOS, and more.
    • Beginner Friendly.
      12 hours

Syntax

dictionaryInstance.isEmpty

Example

var bakery = [String:Int]()
print(bakery.isEmpty)
bakery["Cupcakes"] = 12
print(bakery.isEmpty)

In the example above, the bakery dictionary is checked if it .isEmpty; once when it is initiated, and again when a key-value pair is added. This will output:

true
false

All contributors

Contribute to Docs

Learn Swift on Codecademy

  • Learn how to build iOS applications with Swift and SwiftUI and publish them to Apples' App Store.
    • Includes 7 Courses
    • With Certificate
    • Beginner Friendly.
      13 hours
  • A powerful programming language developed by Apple for iOS, macOS, and more.
    • Beginner Friendly.
      12 hours