What is the isStrictSuperset(of: ) method in Swift?

Overview

We use the isStrictSuperset(of:) method when we want to check if a certain set is a strict superset of another. If every member of the latter set is a member of the former, and the former contains at least one element which is not a member of the latter, the former is a superset.

For example, set1 is a strict superset of set2 if all the elements of set2 are present in set1 . And that set1 contains at least one element that is not a part of set2 .

Syntax

set1.isStrictSuperset(of: set2)
Syntax for isStrictSuperset() method

Parameters

This method takes in two parameters:

  • set1: The set we want to check if it is a superset of another set set2.
  • set2: The other set we want to compare with set1.

Return value

The value returned is a boolean value. It returns true if set1 is a strict superset of set2.Otherwise, false is returned.

Code example

Let's look at the code below:

// create some sets
let names: Set = ["Alicia", "Bethany", "Chris", "Diana", "Eric"]
let workers: Set = ["Alicia", "Bethany", "Diana"]
let numbers1: Set = [1, 2, 3, 4, 5, 7, 8, 9, 10]
let numbers2 : Set = [10, 20,1000]
// check for some strict sub sets
print(names.isStrictSuperset(of: workers))
print(numbers2.isStrictSuperset(of: numbers1))

Explanation

  • Lines 2 to 5: We create a couple of sets.
  • Lines 8 and 9: We check if some sets were strict subsets of another using the isStrictSuperset() and then print the results to the console.
  • Free Resources