Swift .isEmpty

Anonymous contributor's avatar
Anonymous contributor
Published Sep 25, 2023
Contribute to Docs

The .isEmpty property returns true if an array contains no elements, and returns false if the array contains one or more elements.

  • 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

arrayName.isEmpty

Where arrayName is the array being checked for elements.

Example

The following example creates and array and evaluates the contents of the array with .isEmpty before and after adding an element:

var favoriteSongs = [String]()
print(favoriteSongs.isEmpty)
favoriteSongs.append("Stayin' Alive")
print(favoriteSongs.isEmpty)

This will output:

true
false

Note: Big-O of this method is O(1).

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