Swift Set first

The first property returns the first element of the set.

Example


first Syntax

The syntax of the set first property is:

set.first 

Here, set is an object of the Set class.


first Return Values

The first property returns the first element of set.

Notes:

  • The first property returns an optional value, so we need to unwrap it. There are different techniques to unwrap optionals. To learn more about optionals, visit Swift Optionals.
  • Since sets are unordered, we will get different values from the first property.

Example: Swift set first

Output

First Name of set: Perry
First Even Number: 6

In the above example, the first element returns the first element of names and even respectively.

Since the sets are unordered, the first element can be any element of the sets. So the first property returns a random element from names and even.