Swift .removeAll()

KyraThompson's avatar
Published Apr 26, 2023Updated May 5, 2023
Contribute to Docs

The .removeAll() method will remove all key-value pairs from a dictionary.

  • 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.removeAll()

Note: The .removeAll() method takes no parameters.

Example

var bookShelf = [
"Moby Dick": "Herman Melville",
"The Odyssey": "Homer",
"Pride and Prejudice": "Jane Austen"
]
bookShelf.removeAll()
print(bookShelf)

In the example above, all key-value pairs in the bookShelf dictionary are removed. This will output an empty dictionary:

[:]

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