avatarBytefer

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

8047

Abstract

e language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">I like <span class="hljs-subst">${fruit}</span>s</span>); }

<span class="hljs-comment">/** Output: "I like Apples" "I like Oranges" "I like Grapess" */</span></pre></div><h1 id="42aa">Dictionaries</h1><p id="b7c1">A dictionary is a collection of key-value pairs that allows you to quickly retrieve values ​​by key.</p><h2 id="3933">1. Creating a Dictionary</h2><p id="28a6">In Swift, dictionaries are declared using the syntax <code>[Key: Value]</code>, where Key is the data type of the key and Value is the data type of the value.</p><p id="19ed"><b>Swift Code</b></p><div id="151a"><pre><span class="hljs-keyword">var</span> studentScores <span class="hljs-operator">=</span> [<span class="hljs-string">"Alice"</span>: <span class="hljs-number">95</span>, <span class="hljs-string">"Bob"</span>: <span class="hljs-number">87</span>, <span class="hljs-string">"Charlie"</span>: <span class="hljs-number">90</span>]</pre></div><p id="7e71"><b>TypeScript Code</b></p><div id="cf2e"><pre><span class="hljs-keyword">let</span> <span class="hljs-attr">studentScores</span>: { [<span class="hljs-attr">key</span>: <span class="hljs-built_in">string</span>]: <span class="hljs-built_in">number</span> } = { <span class="hljs-string">"Alice"</span>: <span class="hljs-number">95</span>, <span class="hljs-string">"Bob"</span>: <span class="hljs-number">87</span>, <span class="hljs-string">"Charlie"</span>: <span class="hljs-number">90</span> };</pre></div><h2 id="ab9c">2. Access and modify dictionary elements</h2><p id="9557">In Swift and TypeScript, you can access and modify dictionary elements by key.</p><p id="850a"><b>Swift Code</b></p><div id="8234"><pre><span class="hljs-keyword">let</span> aliceScore <span class="hljs-operator">=</span> studentScores[<span class="hljs-string">"Alice"</span>]

studentScores[<span class="hljs-string">"Bob"</span>] <span class="hljs-operator">=</span> <span class="hljs-number">92</span> <span class="hljs-comment">// studentScores: ["Alice": 95, "Bob": 92, "Charlie": 90]</span></pre></div><p id="2f83"><b>TypeScript Code</b></p><div id="9aba"><pre><span class="hljs-keyword">const</span> <span class="hljs-attr">aliceScore</span>: <span class="hljs-built_in">number</span> = studentScores[<span class="hljs-string">"Alice"</span>];

studentScores[<span class="hljs-string">"Bob"</span>] = <span class="hljs-number">92</span>; <span class="hljs-comment">// studentScores: {"Alice": 95, "Bob": 92, "Charlie": 90}</span></pre></div><h2 id="916f">3. Add key-value pairs</h2><p id="c3b1">In Swift and TypeScript, key-value pairs are added using the subscript assignment.</p><p id="b469"><b>Swift Code</b></p><div id="d1fe"><pre>studentScores[<span class="hljs-string">"David"</span>] <span class="hljs-operator">=</span> <span class="hljs-number">88</span> <span class="hljs-comment">// studentScores: ["Charlie": 90, "Alice": 95, "Bob": 92, "David": 88]</span></pre></div><p id="a61a"><b>TypeScript Code</b></p><div id="e52d"><pre>studentScores[<span class="hljs-string">"David"</span>] = <span class="hljs-number">88</span>; <span class="hljs-comment">// studentScores: {"Charlie": 90, "Alice": 95, "Bob": 92, "David": 88}</span></pre></div><h2 id="7fcd">4. Delete key-value pair</h2><p id="994d">In Swift, the <code>removeValue(forKey:)</code> method removes a key-value pair for a specified key. In TypeScript, the <code>delete</code>operator is used to remove a key-value pair.</p><p id="836c"><b>Swift Code</b></p><div id="bb7a"><pre>studentScores.removeValue(forKey: <span class="hljs-string">"Charlie"</span>) <span class="hljs-comment">// studentScores: ["David": 88, "Bob": 92, "Alice": 95]</span></pre></div><p id="e26b"><b>TypeScript Code</b></p><div id="2751"><pre><span class="hljs-keyword">delete</span> studentScores[<span class="hljs-string">"Charlie"</span>]; <span class="hljs-comment">// studentScores: {"David": 88, "Bob": 92, "Alice": 95}</span></pre></div><h2 id="02b7">5. Get a collection of keys and values</h2><p id="e597">In Swift, the <code>keys</code> and <code>values</code> properties are used to get the set of keys and values of a dictionary. In TypeScript, you use the <code>Object.keys</code> and <code>Object.values</code> methods.</p><p id="ef63"><b>Swift Code</b></p><div id="4315"><pre><span class="hljs-keyword">let</span> allKeys <span class="hljs-operator">=</span> <span class="hljs-type">Array</span>(studentScores.keys) <span class="hljs-keyword">let</span> allValues <span class="hljs-operator">=</span> <span class="hljs-type">Array</span>(studentScores.values) <span class="hljs-comment">// allKeys: ["David", "Bob", "Alice"]</span> <span class="hljs-comment">// allValues: [88, 92, 95]</span></pre></div><p id="4519"><b>TypeScript Code</b></p><div id="e414"><pre><span class="hljs-keyword">const</span> <span class="hljs-attr">allKeys</span>: <span class="hljs-built_in">string</span>[] = <span class="hljs-title class_">Object</span>.<span class="hljs-title function_">keys</span>(studentScores); <span class="hljs-keyword">const</span> <span class="hljs-attr">allValues</span>: <span class="hljs-built_in">number</span>[] = <span class="hljs-title class_">Object</span>.<span class="hljs-title function_">values</span>(studentScores); <span class="hljs-comment">// allKeys: ["Alice", "Bob", "David"] </span> <span class="hljs-comment">// allValues: [95, 92, 88]</span></pre></div><h2 id="a1d7">6. Traverse dictionary</h2><p id="8a51">In both Swift and TypeScript, dictionaries can be traversed in <code>for-in</code> loops.</p><p id="0a0f"><b>Swift Code</b></p><div id="fcc3"><pre><span class="hljs-keyword">for</span> (name, score) <span class="hljs-keyword">in</span> studentScores { <span class="hljs-built_in">print</span>(<span class="hljs-string">"<span class="hljs-subst">(name)</span> scored <span class="hljs-subst">(score)</span>"</span>) }

<span class="hljs-comment">/** Output: Alice scored 95 Bob scored 92 David scored 88 */</span></pre></div><p id="dd35"><b>TypeScript Code</b></p><div id="0009"><pre><span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> name <span class="hljs-keyword">in</span> studentScores) { <span class="hljs-keyword">const</span> <span class="hljs-attr">score</span>: <span class="hljs-built_in">number</span> = studentScores[name]; <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string"><span class="hljs-subst">${name}</span> scored <span class="hljs-subst">${score}</span></span>); }

<span class="hljs-comment">/** Output: "Alice scored 95" "Bob scored 92" "David scored 88" */</span></pre></div><h1 id="fa93">Sets</h1><p id="6a28">A set is an unordered collection type with no duplicate elements. In Swift, collections are represented by the Set type.</p><h2 id="660e">1. Creating a set</h2><p id="56c3"><b>Swift Code</b></p><div id="95c0"><pre><span class="hljs-keyword">var</span> <span class="hljs-attr">numberSet</span>: <span class="hljs-title class_">Set</span><<span class="hljs-title class_">Int</span>> = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>]</pre></div><p id="e56c"><b>TypeScript Code</b></p><div id="6603"><pre><span class="hljs-keyword">let</span> <span class="hljs-attr">numberSet</span>: <span class="hljs-title class_">Set</span><<span class="hljs-built_in">number</span>> = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Set</span>([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>]);</pre></div><h2 id="0d5c">2. Adding to a set</h2><p id="4033">In Swift, use the <code>insert</code> method to add elements to a collection. In TypeScript, use the <code>add</code> method

Options

.</p><p id="ef8e"><b>Swift Code</b></p><div id="c1b1"><pre>numberSet.insert(<span class="hljs-number">6</span>) <span class="hljs-comment">// numberSet: [3, 4, 5, 1, 2, 6]</span></pre></div><p id="9349"><b>TypeScript Code</b></p><div id="aaeb"><pre>numberSet.<span class="hljs-title function_">add</span>(<span class="hljs-number">6</span>); <span class="hljs-comment">// numberSet: {1, 2, 3, 4, 5, 6}</span></pre></div><h2 id="aa33">3. Removing an element from a set</h2><p id="7cf0">In Swift, you use the <code>remove</code> method to remove an element from a collection. In TypeScript, the <code>delete</code> method is used.</p><p id="d9e7"><b>Swift Code</b></p><div id="f255"><pre>numberSet.remove(<span class="hljs-number">3</span>) <span class="hljs-comment">// numberSet: [5, 2, 1, 4, 6]</span></pre></div><p id="9a30"><b>TypeScript Code</b></p><div id="bce7"><pre>numberSet.<span class="hljs-title function_">delete</span>(<span class="hljs-number">3</span>); <span class="hljs-comment">// numberSet: {1, 2, 4, 5, 6} </span></pre></div><h2 id="7b5d">4. Looping through a set</h2><p id="0ffc">In Swift, you can iterate through a collection using a <code>for-in</code> loop. In TypeScript, you can iterate through collections using the <code>forEach</code> method.</p><p id="2104"><b>Swift Code</b></p><div id="5497"><pre><span class="hljs-keyword">for</span> number <span class="hljs-keyword">in</span> numberSet { <span class="hljs-built_in">print</span>(<span class="hljs-string">"Number is <span class="hljs-subst">(number)</span>"</span>) }

<span class="hljs-comment">/** Output: Number is 2 Number is 1 Number is 5 Number is 6 Number is 4 */</span></pre></div><p id="22f0"><b>TypeScript Code</b></p><div id="4046"><pre>numberSet.<span class="hljs-title function_">forEach</span>(<span class="hljs-function">(<span class="hljs-params"><span class="hljs-built_in">number</span></span>) =></span> { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">Number is <span class="hljs-subst">${<span class="hljs-built_in">number</span>}</span></span>); });

<span class="hljs-comment">/** Output: "Number is 1" "Number is 2" "Number is 4" "Number is 5" "Number is 6" */</span></pre></div><h2 id="8c10">5. Intersections</h2><p id="f521">In Swift, you use the <code>intersection</code> method to get the intersection of two sets. In TypeScript, you can use the Set constructor and the <code>filter</code> method.</p><figure id="aab5"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*i3aIEXpMkmxJ6Wdoy8lElw.jpeg"><figcaption></figcaption></figure><p id="587a"><b>Swift Code</b></p><div id="2698"><pre><span class="hljs-keyword">let</span> anotherSet: <span class="hljs-type">Set</span><<span class="hljs-type">Int</span>> <span class="hljs-operator">=</span> [<span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>] <span class="hljs-keyword">let</span> intersection <span class="hljs-operator">=</span> numberSet.intersection(anotherSet)

<span class="hljs-comment">// numberSet: [1, 5, 4, 2, 6]</span> <span class="hljs-comment">// anotherSet: [5, 4, 6]</span> <span class="hljs-comment">// intersection: [5, 4, 6]</span></pre></div><p id="ee05"><b>TypeScript Code</b></p><div id="b08d"><pre><span class="hljs-keyword">const</span> <span class="hljs-attr">anotherSet</span>: <span class="hljs-title class_">Set</span><<span class="hljs-built_in">number</span>> = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Set</span>([<span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>]); <span class="hljs-keyword">const</span> <span class="hljs-attr">intersection</span>: <span class="hljs-title class_">Set</span><<span class="hljs-built_in">number</span>> = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Set</span>([...numberSet].<span class="hljs-title function_">filter</span>(<span class="hljs-function"><span class="hljs-params">x</span> =></span> anotherSet.<span class="hljs-title function_">has</span>(x)));

<span class="hljs-comment">// numberSet: {1, 2, 4, 5, 6} </span> <span class="hljs-comment">// anotherSet: {4, 5, 6}</span> <span class="hljs-comment">// intersection: {4, 5, 6}</span></pre></div><h2 id="180a">6. Unions</h2><p id="866d">In Swift, use the <code>union</code> method to get the union of two collections.</p><figure id="2135"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*81DLq-4lm9Y6RwoDktPMEA.jpeg"><figcaption></figcaption></figure><p id="6fe3"><b>Swift Code</b></p><div id="2adf"><pre><span class="hljs-keyword">let</span> union <span class="hljs-operator">=</span> numberSet.union(anotherSet)

<span class="hljs-comment">// numberSet: [5, 1, 4, 2, 6]</span> <span class="hljs-comment">// anotherSet: [4, 5, 6]</span> <span class="hljs-comment">// union: [5, 1, 4, 2, 6]</span></pre></div><p id="0603"><b>TypeScript Code</b></p><div id="8c25"><pre><span class="hljs-keyword">const</span> <span class="hljs-attr">union</span>: <span class="hljs-title class_">Set</span><<span class="hljs-built_in">number</span>> = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Set</span>([...numberSet, ...anotherSet]);

<span class="hljs-comment">// numberSet: {1, 2, 4, 5, 6} </span> <span class="hljs-comment">// anotherSet: {4, 5, 6}</span> <span class="hljs-comment">// union: {1, 2, 4, 5, 6} </span></pre></div><h2 id="c0ce">7. Difference of Sets</h2><p id="8ff3">In Swift, you use the <code>subtracting</code> method to get the difference between two sets. In TypeScript, you can use the Set constructor and the <code>filter</code> method.</p><figure id="f275"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*TB-XQzh2j5zj4E6kkrrUhw.jpeg"><figcaption></figcaption></figure><p id="54f6"><b>Swift Code</b></p><div id="4fa5"><pre><span class="hljs-keyword">let</span> difference <span class="hljs-operator">=</span> numberSet.subtracting(anotherSet)

<span class="hljs-comment">// numberSet: [6, 2, 4, 1, 5]</span> <span class="hljs-comment">// anotherSet: [5, 6, 4]</span> <span class="hljs-comment">// difference: [1, 2]</span></pre></div><p id="4b71"><b>TypeScript Code</b></p><div id="d7eb"><pre><span class="hljs-keyword">const</span> <span class="hljs-attr">difference</span>: <span class="hljs-title class_">Set</span><<span class="hljs-built_in">number</span>> = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Set</span>([...numberSet].<span class="hljs-title function_">filter</span>(<span class="hljs-function"><span class="hljs-params">x</span> =></span> !anotherSet.<span class="hljs-title function_">has</span>(x)));

<span class="hljs-comment">// numberSet: {1, 2, 4, 5, 6} </span> <span class="hljs-comment">// anotherSet: {4, 5, 6}</span> <span class="hljs-comment">// difference: {1, 2} </span></pre></div><p id="aa3a">In this article, we introduce the collection types Arrays, Dictionaries, and Sets. By comparing it to TypeScript syntax, we hope to help you better understand the features of Swift. If you want to learn Swift, follow me on <a href="https://medium.com/@bytefer">Medium</a> or <a href="https://twitter.com/Tbytefer">Twitter</a> to read more about Swift and TS!</p><div id="1440" class="link-block"> <a href="https://medium.com/@bytefer/list/688ee7c12807"> <div> <div> <h2>Mastering TypeScript Series</h2> <div><h3>This series will introduce the core knowledge and techniques of TypeScript in the form of animations.</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*624748c44fc686389a9ed7259663c489edb2ceb7.jpeg)"></div> </div> </div> </a> </div></article></body>

Swift Tutorials for Front-end Developers: Arrays, Dictionaries and Sets

Master Swift in 2024 and Start Developing Your Own iOS/macOS App

Swift is a general-purpose programming language that’s approachable for newcomers and powerful for experts.It is fast, modern, safe, and a joy to write.

Welcome to the Mastering Swift tutorial series, in this article, we will introduce the three collection types Arrays, Dictionaries, and Sets in Swift. If you still need to install Xcode and configure your Swift development environment, please read the following article first.

Next, we launch Xcode and select “File” > “New” > “Playground”. Create a new Playground and name it “Collections”.

Arrays

An array is an ordered collection of elements of the same type and is one of Swift's commonly used data structures.

1. Creating an array

Swift Code

var numbers = [1, 2, 3, 4, 5]
// Or var numbers: [Int] = [1, 2, 3, 4, 5]

var fruits = ["Apple", "Banana", "Orange"]
// Or var fruits: [String] = ["Apple", "Banana", "Orange"]

TypeScript Code

let numbers = [1, 2, 3, 4, 5];
// Or let numbers: number[] = [1, 2, 3, 4, 5];

let fruits = ["Apple", "Banana", "Orange"];
// Or let fruits: string[] = ["Apple", "Banana", "Orange"];

2. Accessing and Modifying Array Element

In Swift and TypeScript, you can access and modify array elements via subscripts.

Swift Code

let firstNumber = numbers[0]
// firstNumber: 1

numbers[1] = 6

TypeScript Code

const firstNumber = numbers[0];
// firstNumber: 1

numbers[1] = 6;

3. Adding an item to the array

In Swift, you add an element using the append method. In TypeScript, you use the push method.

Swift Code

fruits.append("Grapes")
// fruits: ["Apple", "Banana", "Orange", "Grapes"]

TypeScript Code

fruits.push("Grapes");
// fruits: ["Apple", "Banana", "Orange", "Grapes"] 

4. Removing an item from the array

In Swift, the remove(at:) method removes the element at the specified position. In TypeScript, the splice method is used.

Swift Code

fruits.remove(at: 1)
// fruits: ["Apple", "Orange", "Grapes"]

TypeScript Code

fruits.splice(1, 1);
// fruits: ["Apple", "Orange", "Grapes"]

5. Counting items in the array

In Swift, you use the count property to get the length of an array. In TypeScript, you use the length property.

Swift Code

let count = fruits.count
// print("Count: \(fruits.count)")

TypeScript Code

const count = fruits.length;
// console.log(`Count: ${fruits.length}`)

6. Iterate over array elements

In Swift, you can use for-in to iterate through an array. In TypeScript, you can use for-of loops.

Swift Code

for fruit in fruits {
    print("I like \(fruit)s")
}

/**
Output:
I like Apples
I like Oranges
I like Grapess
*/

TypeScript Code

for (const fruit of fruits) {
    console.log(`I like ${fruit}s`);
}

/**
Output:
"I like Apples" 
"I like Oranges" 
"I like Grapess" 
*/

Dictionaries

A dictionary is a collection of key-value pairs that allows you to quickly retrieve values ​​by key.

1. Creating a Dictionary

In Swift, dictionaries are declared using the syntax [Key: Value], where Key is the data type of the key and Value is the data type of the value.

Swift Code

var studentScores = ["Alice": 95, "Bob": 87, "Charlie": 90]

TypeScript Code

let studentScores: { [key: string]: number } = { "Alice": 95, "Bob": 87, "Charlie": 90 };

2. Access and modify dictionary elements

In Swift and TypeScript, you can access and modify dictionary elements by key.

Swift Code

let aliceScore = studentScores["Alice"]

studentScores["Bob"] = 92
// studentScores: ["Alice": 95, "Bob": 92, "Charlie": 90]

TypeScript Code

const aliceScore: number = studentScores["Alice"];

studentScores["Bob"] = 92;
// studentScores: {"Alice": 95, "Bob": 92, "Charlie": 90}

3. Add key-value pairs

In Swift and TypeScript, key-value pairs are added using the subscript assignment.

Swift Code

studentScores["David"] = 88
// studentScores: ["Charlie": 90, "Alice": 95, "Bob": 92, "David": 88]

TypeScript Code

studentScores["David"] = 88;
// studentScores: {"Charlie": 90, "Alice": 95, "Bob": 92, "David": 88}

4. Delete key-value pair

In Swift, the removeValue(forKey:) method removes a key-value pair for a specified key. In TypeScript, the deleteoperator is used to remove a key-value pair.

Swift Code

studentScores.removeValue(forKey: "Charlie")
// studentScores: ["David": 88, "Bob": 92, "Alice": 95]

TypeScript Code

delete studentScores["Charlie"];
// studentScores: {"David": 88, "Bob": 92, "Alice": 95}

5. Get a collection of keys and values

In Swift, the keys and values properties are used to get the set of keys and values of a dictionary. In TypeScript, you use the Object.keys and Object.values methods.

Swift Code

let allKeys = Array(studentScores.keys)
let allValues = Array(studentScores.values)
// allKeys: ["David", "Bob", "Alice"]
// allValues: [88, 92, 95]

TypeScript Code

const allKeys: string[] = Object.keys(studentScores);
const allValues: number[] = Object.values(studentScores);
// allKeys: ["Alice", "Bob", "David"] 
// allValues: [95, 92, 88]

6. Traverse dictionary

In both Swift and TypeScript, dictionaries can be traversed in for-in loops.

Swift Code

for (name, score) in studentScores {
    print("\(name) scored \(score)")
}

/**
Output:
Alice scored 95
Bob scored 92
David scored 88
*/

TypeScript Code

for (const name in studentScores) {
    const score: number = studentScores[name];
    console.log(`${name} scored ${score}`);
}

/**
Output:
"Alice scored 95" 
"Bob scored 92"
"David scored 88" 
*/

Sets

A set is an unordered collection type with no duplicate elements. In Swift, collections are represented by the Set type.

1. Creating a set

Swift Code

var numberSet: Set<Int> = [1, 2, 3, 4, 5]

TypeScript Code

let numberSet: Set<number> = new Set([1, 2, 3, 4, 5]);

2. Adding to a set

In Swift, use the insert method to add elements to a collection. In TypeScript, use the add method.

Swift Code

numberSet.insert(6)
// numberSet: [3, 4, 5, 1, 2, 6]

TypeScript Code

numberSet.add(6);
// numberSet: {1, 2, 3, 4, 5, 6}

3. Removing an element from a set

In Swift, you use the remove method to remove an element from a collection. In TypeScript, the delete method is used.

Swift Code

numberSet.remove(3)
// numberSet: [5, 2, 1, 4, 6]

TypeScript Code

numberSet.delete(3);
// numberSet: {1, 2, 4, 5, 6} 

4. Looping through a set

In Swift, you can iterate through a collection using a for-in loop. In TypeScript, you can iterate through collections using the forEach method.

Swift Code

for number in numberSet {
    print("Number is \(number)")
}

/**
Output:
Number is 2
Number is 1
Number is 5
Number is 6
Number is 4
*/

TypeScript Code

numberSet.forEach((number) => {
    console.log(`Number is ${number}`);
});

/**
Output:
"Number is 1"
"Number is 2"
"Number is 4"
"Number is 5"
"Number is 6"
*/

5. Intersections

In Swift, you use the intersection method to get the intersection of two sets. In TypeScript, you can use the Set constructor and the filter method.

Swift Code

let anotherSet: Set<Int> = [4, 5, 6]
let intersection = numberSet.intersection(anotherSet)

// numberSet: [1, 5, 4, 2, 6]
// anotherSet: [5, 4, 6]
// intersection: [5, 4, 6]

TypeScript Code

const anotherSet: Set<number> = new Set([4, 5, 6]);
const intersection: Set<number> = new Set([...numberSet].filter(x => anotherSet.has(x)));

// numberSet: {1, 2, 4, 5, 6} 
// anotherSet: {4, 5, 6}
// intersection: {4, 5, 6}

6. Unions

In Swift, use the union method to get the union of two collections.

Swift Code

let union = numberSet.union(anotherSet)

// numberSet: [5, 1, 4, 2, 6]
// anotherSet: [4, 5, 6]
// union: [5, 1, 4, 2, 6]

TypeScript Code

const union: Set<number> = new Set([...numberSet, ...anotherSet]);

// numberSet: {1, 2, 4, 5, 6} 
// anotherSet: {4, 5, 6}
// union: {1, 2, 4, 5, 6} 

7. Difference of Sets

In Swift, you use the subtracting method to get the difference between two sets. In TypeScript, you can use the Set constructor and the filter method.

Swift Code

let difference = numberSet.subtracting(anotherSet)

// numberSet: [6, 2, 4, 1, 5]
// anotherSet: [5, 6, 4]
// difference: [1, 2]

TypeScript Code

const difference: Set<number> = new Set([...numberSet].filter(x => !anotherSet.has(x)));

// numberSet: {1, 2, 4, 5, 6} 
// anotherSet: {4, 5, 6}
// difference: {1, 2} 

In this article, we introduce the collection types Arrays, Dictionaries, and Sets. By comparing it to TypeScript syntax, we hope to help you better understand the features of Swift. If you want to learn Swift, follow me on Medium or Twitter to read more about Swift and TS!

Programming
Web Development
JavaScript
Typescript
Swift
Recommended from ReadMedium