Key Concepts

Review core concepts you need to learn to master this subject

Array

var scores = [Int]() // The array is empty: []

An array stores an ordered collection of values of the same data type.

Use the initializer syntax, [Type](), to create an empty array of a certain type.

Initialize with Array Literal

var scores = [Int]() // The array is empty: []

An array can be initialized with an array literal, which is a short-hand method of writing one or more values as an array collection.

An array literal is written as a list of values, separated by commas, and surrounded by a pair of square brackets.

Index

var scores = [Int]() // The array is empty: []

An index refers to an item’s position within an ordered list. Use the subscript syntax, array[index], to retrieve an individual element from an array.

Note: Swift arrays are zero-indexed, meaning the first element has index 0.

.count Property

var scores = [Int]() // The array is empty: []

The .count property returns the number of elements in an array.

.append() Method and += Operator

var scores = [Int]() // The array is empty: []

The .append() method can be called on an array to add an item to the end of the array.

The += addition assignment operator can be used to add the elements of another array to the existing array.

.insert() and .remove() Methods

var scores = [Int]() // The array is empty: []

The .insert() method can be called on an array to add an element at a specified index. It takes two arguments: value and at: index.

The .remove() method can be called on an array to remove an element at a specified index. It takes one argument: at: index.

Iterating Over an Array

var scores = [Int]() // The array is empty: []

In Swift, a for-in loop can be used to iterate through the items of an array.

This is a powerful tool for working with and manipulating a large amount of data.

Swift Sets

var scores = [Int]() // The array is empty: []

We can use a set to store unique elements of the same data type.

Empty Sets

var scores = [Int]() // The array is empty: []

An empty set is a set that contains no values inside of it.

Populated Sets

var scores = [Int]() // The array is empty: []

To create a set populated with values, use the Set keyword before the assignment operator.

The values of the set must be contained within brackets [] and separated with commas ,.

.insert()

var scores = [Int]() // The array is empty: []

To insert a single value into a set, append .insert() to a set and place the new value inside the parentheses ().

.remove() and .removeAll() Methods

var scores = [Int]() // The array is empty: []

To remove a single value from a set, append .remove() to a set with the value to be removed placed inside the parentheses ().

To remove every single value from a set at once, append .removeAll() to a set.

.contains()

var scores = [Int]() // The array is empty: []

Appending .contains() to an existing set with an item in the parentheses () will return a true or false value that states whether the item exists within the set.

Iterating Over a Set

var scores = [Int]() // The array is empty: []

A for-in loop can be used to iterate over each item in a set.

.isEmpty Property

var scores = [Int]() // The array is empty: []

Use the built-in property .isEmpty to check if a set has no values contained in it.

.count Property

var scores = [Int]() // The array is empty: []

The property .count returns the number of elements contained within a set.

.intersection() Operation

var scores = [Int]() // The array is empty: []

The .intersection() operation populates a new set of elements with the overlapping elements of two sets.

.union() Operation

var scores = [Int]() // The array is empty: []

The .union() operation populates a new set by taking all the values from two sets and combining them.

.symmetricDifference() Operation

var scores = [Int]() // The array is empty: []

The .symmetricDifference() operation creates a new set with all the non-overlapping values between two sets.

.subtracting() Operation

var scores = [Int]() // The array is empty: []

The .subtracting() operation removes the values of one second set from another set and stores the remaining values in a new set.

  1. 1
    We live in an increasingly connected world where information flows between us and the apps that we interact with every day. Historically that information was stored in filing cabinets but, today, m…
  2. 2
    In Swift, an array stores values of the same type in an ordered list. To create an empty array of a certain type, we can use the initializer syntax: var name = Type Suppose we want to crea…
  3. 3
    We learned one way to create an array, but there’s another. We can also declare and initialize an array with a list of values, separated by commas, and surrounded by a pair of square brackets. Th…
  4. 4
    Now that we have an array, how do we retrieve an individual value? This is where index comes into play. An index refers to an item’s position within an ordered list. Arrays in Swift are zero-ind…
  5. 5
    Properties in Swift, allow us to access a value that is specific to a particular structure. We can use the .count property o…
  6. 6
    When working with arrays, we might need to add additional elements to it after initialization. We can add a new item to the end of an array by calling the array’s [.append()](https://developer.app…
  7. 7
    Previously, we learned how to add an item to the end of the array, but how do we add an item in the middle or even the start? And how do we remove an item? Suppose we have an array: var moon = [“…
  8. 8
    What happens if we want to iterate through items of an array? We can use a for-in loop! The syntax looks like the following: for item in array { // Loop body } For example, suppose we have a …
  9. 9
    [“Hip”, “hip”, “array!”] We have learned about how to store groups of data into arrays in Swift. 🙌 Here are some of the things that we covered: - An array is a list of ordered items. var evenNumb…
  1. 1
    Imagine walking through an art museum and finding two identical Mona Lisa paintings hanging on the wall. That situation would most likely cause a few raised eyebrows. Art museums should only cont…
  2. 2
    In Swift, we can initialize sets that are either empty or populated with values. A set that has been initialized without any values inside of it is known as an empty set. Empty sets are useful …
  3. 3
    We can use different built-in properties to help us understand information about specific collection types, like sets or arrays, in our programs! For example, we can use .count to find out how man…
  4. 4
    If we want to add new elements to our set, we can use Swift’s built-in .insert() method. Being able to augment our sets with new elements is useful when we have additional data to store or when w…
  5. 5
    If we need to remove an element from a set, we can use .remove(). This method can remove a single item from a set with the following syntax: setName.remove(Value) Let’s revisit the set plantShelf…
  6. 6
    If we want to check whether or not an element exists within a set, we can use the .contains() method. We can use the following syntax to check for an element inside of a set: setName.contains(Valu…
  7. 7
    If we want to go through every individual value contained in a set, we can use a for-in loop! To iterate over every item of a set, we can use this syntax: for Value in setName { // Body of loop …
  8. 8
    In Swift, we can use set operations to create new sets based on the data within two different sets! This is useful when we are working with multiple data sets. We can use the .intersection() metho…
  9. 9
    Another set operation we can use is .union(). This operation creates a set by combining the values of two sets together. The syntax for creating a set using .union() is: var NewSet = SetA.union(Se…
  10. 10
    The .symmetricDifference() operation can be used to find elements that exist in one of the given sets, but not both. The syntax for creating a set using .symmetricDifference() looks like this: var…
  11. 11
    Another set operation we can use is .subtracting(). This method creates a new set of elements by removing the overlapping data of one set from another set. To use .subtracting() in our program, w…
  12. 12
    Great job completing this lesson! Let’s go over what we learned: * A set is an unordered collection of unique elements. * We have the option to initialize sets that are either empty or populated: …

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo