Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

set
first
swift
communitycreator

What is the first property of a set in Swift?

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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!)
Using the first property on a set in Swift

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.

RELATED TAGS

set
first
swift
communitycreator

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring