Apple Compatibility

Swift Set

If you are working on Swift set collections, in some cases, you need to add/insert a particular element to the set collection. In that case, using the insert() method is the best option.

It adds the element in the last position. It is possible for this method to take only one parameter, i.e. element. So, it adds this element to the existing set. If the Swift set is empty, the insert() method works and adds the elements to the set.

Syntax:

swift_set.insert(element)

 
Where the swift_set is the set and element is the value/string added to the swift_set.

Example 1:

Let’s create a Swift set with String type and add an element.

//create a swift Set with String type that has 3 elements
var swift_set1:Set<String> = ["swift1","swift2","swift3"]

print("Actual Set - \(swift_set1)")

//add element-"swift1"
swift_set1.insert("swift4")

print("After adding swift-4 - \(swift_set1)")

 
Output:

Explanation:

Line 2:


Swift_set1 has 3 strings.

Line 7:


Using the insert() method, we add the “swift4” to the swift_set1.

Line 9:


Finally, we displaythe swift_set1. You can see that swift-4 is added successfully.

Example 2:

Let’s create a Swift set with Integer type and add the elements.

//create a swift Set with Integer type that has 5 elements
var swift_set1:Set<Int> = [10,20,45,67,89]

print("Actual Set - \(swift_set1)")

//add element-500
swift_set1.insert(500)

print("After adding 500 - \(swift_set1)")

//add element-800
swift_set1.insert(800)

print("After adding 800 - \(swift_set1)")

//add element-1000
swift_set1.insert(1000)

print("After adding 1000 - \(swift_set1)")

 
Output:


As the Swift set is an unordered collection, it does not display all the elements in the same order every time.

Explanation:

Line 2:


Swift_set1 has 5 Integer values.

Line 7-19:


We add the following values one by one to the swift_set1 using the insert() method and display the swift_set1 each time.

Example 3:

Let’s create a Swift set with Integer type and add the elements.

//create a swift Set with Integer type that has no elements
var swift_set1:Set<Int> = []

print("Actual Set - \(swift_set1)")

//add element-500
swift_set1.insert(500)

print("After adding 500 - \(swift_set1)")

//add element-800
swift_set1.insert(800)

print("After adding 800 - \(swift_set1)")

//add element-1000
swift_set1.insert(1000)

print("After adding 1000 - \(swift_set1)")

 
Output:


As the Swift set is an unordered collection, it does not display all the elements in the same order every time.

Explanation:

Line 2:


Initially, swift_set1 has no elements.

Line 7-19:


We add the following values one by one to the swift_set1 using the insert() method and display the swift_set1 each time.

Now you can see the output. There are 3 elements in the swift_set1.

Example 4:

Let’s create a Swift set with Double type and add the elements.

//create a swift Set with Double type that has no elements
var swift_set1:Set<Double> = []

print("Actual Set - \(swift_set1)")

//add element-500
swift_set1.insert(500)

print("After adding 500 - \(swift_set1)")

//add element-800
swift_set1.insert(800)

print("After adding 800 - \(swift_set1)")

//add element-1000
swift_set1.insert(1000)

print("After adding 1000 - \(swift_set1)")

 
Output:


As the Swift set is an unordered collection, it does not display all the elements in the same order every time.

Explanation:

Line 2:


Initially, swift_set1 has no elements.

Line 7-19:


We add the following values one by one to the swift_set1 using the insert() method and display the swift_set1 each time.

Now, you can see the output. There are 3 elements in swift_set1.

Conclusion

In this swift guide, we learned how to add a particular element at the last position using the insert() method. It is possible for this method to take only one parameter, i.e. element. So, it adds this element to the existing set. If the Swift set is empty, the insert() method works and adds the elements to the set. We have seen all the scenarios to insert the elements to the swift set.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain