Swift .count

Sriparno08's avatar
Published Nov 30, 2024
Contribute to Docs

In Swift, the .count property calculates the total number of existing elements in a set.

  • 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.count
  • setName: The name of the set whose elements are being counted.

Example

The following example demonstrates the usage of the .count property:

let nums: Set = [1, 2, 3, 4, 5, 6]
let res = nums.count
print(res)

The above code produces the following output:

6

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