Swift Set update()

In this tutorial, we will learn about the Swift set update() method with the help of examples.

The update() method inserts the given element in the set.

Example

// create a dictionary with two elements
var information: Set = [54, 34, 35]

// insert 76 to information
information.update(with: 76)

// print updated dictionary
print(information)

// Output: [34, 54, 76, 35]

update() Syntax

The syntax of the update() method is:

set.update(with: newElement)

Here, set is an object of the Set class.


update() Parameters

The update() method takes one parameter:

  • newElement - the new element to insert in set

update() Return Value

  • The update() method doesn't return any value. It just updates set.

Example: Swift Set update()

// create a set with three elements
var employees: Set = ["Sabby", "Nick", "Cathy"]

print("Before:", employees)

// insert "Katty" to employees
employees.update(with: "Katty")

// print updated set
print("Updated:", employees)

Output

Before: ["Cathy", "Sabby", "Nick"]
Updated: ["Katty", "Cathy", "Sabby", "Nick"]
Did you find this article helpful?