Swift .removeAll()

YKWIBN's avatar
Published Jan 23, 2023
Contribute to Docs

The .removeAll() method is used to remove every value from a Set in Swift.

  • 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

setName.removeAll()
  • .removeAll() will remove all values from setName.
  • It is not possible to use a (where:) condition on a set because Set does not currently conform to the RangeReplaceableCollection protocol that includes that method.

Example

The following is an example of the .removeAll() method:

var weather: Set = ["spring", "summer", "autumn", "fall", "winter"]
weather.removeAll()
print(weather)

This will output:

[]

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