Swift Set insert()

The insert() method adds a new element at the end of the set.

Example

var city: Set = ["Boston", "Tokyo", "Kathmandu"]

// add "London" to the city set city.insert("London")
print(city) // Output: ["Kathmandu", "Tokyo", "Boston", "London"]

insert() Syntax

The syntax of the set insert() method is:

set.insert(newElement)

Here, set is an object of the set class.


insert() Parameters

The insert() method takes a single parameter:

  • newElement - element to be added to set

insert() Return Value

The insert() method doesn't return any value. It only updates the current set.


Example: Swift set insert()

var languages: Set = ["Swift", "C", "Java"]

// add "C++" to the languages set languages.insert("C++")
print(languages) var priceList: Set = [12, 21, 35]
// add 44 to the priceList set priceList.insert(44)
print(priceList)

Output

["C", "Java", "Swift", "C++"]
[44, 21, 12, 35]
Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges