What is the first property of a set in Swift?

Overview

The first property of a set in Swift returns the first element of the set. Note that this first element is not necessarily the element that was added first to the set. We should not expect any particular ordering of a set in Swift.

Syntax

set.first
Syntax for first property of a set in Swift

Return value

The value returned is an element of the set that is accepted as the first.

Example

// create some sets
let evenNumbers : Set = [10, 2, 6, 8, 4]
let oddNumbers : Set = [11, 5, 7, 3, 9]
let first_names : Set = ["john", "jane", "henry", "mark"]
// get the first elements of the sets
print(evenNumbers.first!)
print(oddNumbers.first!)
print(first_names.first!)

Explanation

  • Lines 2–4: We create some set instances.
  • Lines 6–8: We use the first property to get the first element of the sets. We then print the results to the console.

New on Educative
Learn any Language for FREE all September 🎉
For the entire month of September, get unlimited access to our entire catalog of beginner coding resources.
🎁 G i v e a w a y
30 Days of Code
Complete Educative’s daily coding challenge every day in September, and win exciting Prizes.

Free Resources