Reference Materials
Certification Courses
Created with over a decade of experience and thousands of feedback.
Python online compiler
JavaScript online compiler
SQL online compiler
Java online compiler
HTML online compiler
C online compiler
C++ online compiler
C# online compiler
PHP online compiler
Swift online compiler
Kotlin online compiler
TypeScript online compiler
Go online compiler
Rust online compiler
Scala online compiler
Dart online compiler
R online compiler
Ruby online compiler
Swift Set insert()
In this tutorial, we will learn about the Swift Set insert() method with the help of examples.
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?