Back to Course

Introduction to Swift

0% Complete
0/0 Steps
  1. Swift: Basics
    4 Topics
    |
    5 Quizzes
  2. Swift: Variables
    7 Topics
    |
    8 Quizzes
  3. Swift: Conditional Statements
    10 Topics
    |
    11 Quizzes
  4. Swift: Loops
    9 Topics
    |
    10 Quizzes
  5. Swift: Logical Operators
    6 Topics
    |
    7 Quizzes
  6. Swift: Arrays
    8 Topics
    |
    9 Quizzes
  7. Swift: Sets
    11 Topics
    |
    12 Quizzes
  8. Swift: Dictionaries
    11 Topics
    |
    12 Quizzes
  9. Swift: Functions
    13 Topics
    |
    14 Quizzes
  10. Swift: Structures
    11 Topics
    |
    12 Quizzes
  11. Swift: Classes
    7 Topics
    |
    8 Quizzes
Lesson 7, Topic 11
In Progress

Swift: subtracting Method

Yasin Cakal 28 Dec 2021
Lesson Progress
0% Complete

We can use .subtracting() as a set operation. By removing overlapping data from one set and adding it to another, this method generates a new set of elements.

We can use the following code to use .subtracting() in our program:

var SetSubtracting = SetA.subtracting(SetB)

Any values shared by SetA and SetB will be removed, leaving SetSubtracting with values only found in SetA and not in SetB.

Consider the following two sets: Places and visitedPlaces:

var Places: Set = ["Eiffel Tower", "Great Wall of China", "Kremlin", "Pyramid of Giza", "Taj Mahal", "Statue of Liberty"];
 
var visitedPlaces: Set = ["Taj Mahal", "Great Wall of China", "Kremlin"];

Based on the two sets above, we can use .subtracting() to create a set of places that aren’t visited:

var noVisitedPlaces = Places.subtracting(visitedPlaces);

If we were to output the value of noVisitedPlaces, the results would look something like this:

["Pyramid of Giza", "Eiffel Tower", "Statue of Liberty"]

Responses

Leave a Reply

%d bloggers like this: