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 to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources