KnowledgeCity

Swift Beginner: Complex Data Types

In this module, you will learn about collections and complex data types.

In this module, you will learn about collections and complex data types. Collections are data types that let you store your data in different ways. You’ll also learn about arrays, which keep track of your information in an ordered list. We’ll go over how to append to arrays, delete from arrays, and add elements to a specific number of the array. You’ll also be introduced to dictionaries, a collection type that uses a key-value pair instead of relying on the specific location in the collection.

You’ll learn about enums, which handle related information by creating type common to that specific information, much like the colors of a rainbow and related to each other. The final collection you’ll learn is the set, which is simply a collection of unique elements of data without order. You would use a set when you have no interest in the order of items, but you are interested that each item is different and unique from the others.

Learning Objectives

  • Identify how collections work
  • Understand ordered lists versus unordered lists
  • Recognize key-value pairs

Author: Mark Nair

Duration: 26m · 4 lessons
Level: Beginner
Language: English

Skills you’ll gain

Abstract Data TypesC Data Types (C Programming Language)Data Definition LanguageData Format Description LanguageData StructuresSwift (Programming Language)

What You'll Learn

  • Identify how collections work as data types for storing data in different ways
  • Use arrays to keep track of information in an ordered list, including appending, deleting, and adding elements at a specific position
  • Apply dictionaries to store data as key-value pairs instead of relying on a specific location
  • Create enums to handle related information by defining a type common to that information
  • Use sets to store unique elements of data without order
  • Distinguish ordered lists from unordered lists and recognize key-value pairs

Key Takeaways

  • Collections are data types that let you store your data in different ways.
  • Arrays keep track of information in an ordered list and support appending, deleting, and adding elements at a specific position.
  • Dictionaries are a collection type that uses a key-value pair instead of relying on the specific location in the collection.
  • Enums handle related information by creating a type common to that specific information.
  • A set is a collection of unique elements of data without order, used when item order does not matter but uniqueness does.

Frequently Asked Questions

What collection types does this module cover?

The module covers arrays, dictionaries, enums, and sets, along with collections and complex data types in general.

What will I learn about working with arrays?

You will learn how to append to arrays, delete from arrays, and add elements to a specific position in the array, since arrays keep track of information in an ordered list.

How is a dictionary different from an array?

A dictionary uses a key-value pair instead of relying on the specific location in the collection, whereas an array keeps information in an ordered list.

When would I use a set?

You would use a set when you have no interest in the order of items but you are interested that each item is different and unique from the others, since a set is a collection of unique elements without order.

What are the learning objectives of this module?

The objectives are to identify how collections work, understand ordered lists versus unordered lists, and recognize key-value pairs.

Transcript

Show transcript (free preview lesson)

Transcript of the free preview lesson. Remaining lessons unlock with the full course.

Hello, my name is Mark Nair and in these lessons, you'll learn about complex data types including array, dictionaries, enums, and sets. Collections in Swift let us use many objects as a group instead of having to use different constants or variables to keep track of every individual object. An array is a collection that has an ordered list of values. It looks like this. So here I have a constant called cities. Now that is equal to and I have quite a few names of different cities around the world, but you'll notice that they're in brackets. So different than strings we looked at before, but these are string values all separated by a comma within brackets and this is an array of values. So let's peak inside of cities to see what's in there. To do that, I'll just type cities and I'll run it. Clicking the arrow, you see over here it's the very same values that I have up here in line one. So I know that this is an array. If I option + click cities, let cities type of array of string so it's string inside of these brackets. To find a specific item in the array, we can do this, cities and then bracket and then the number of the item in the array. Now I'm using zero is the first number and you'll see when I run it, the result is Tokyo. The reason why zero is the beginning is because arrays in Swift start at zero. So we start counting zero, one, two, three. A lot of people get confused on this. So just remember in an array, the first item in the array right here, Tokyo is position zero, Delhi position one, Shanghai position two even though it looks like the third thing in the array which it is the third item but it's position zero, one, two. We can check if something is in the array like this. So I'm getting name of cities, the name of the array, dot contains and then in parenthesis, the information that I'm checking for. In this case, it's Taipei. When I run this, I will get true, so this responds with a Boolean value. To change the element in an array, it's the same thing I've done before. The name of the array, brackets, and then the specific location. So this time I will change position one. I will now make this equal to Cairo. Oh, I can't change it because I've made cities a constant, not a variable. So changing that which would help. Now I can change position of one cities to Cairo. Now I see that my order is Tokyo, Cairo. I've replaced Delhi with Cairo. Just keep in mind an array is very specific on where things exist in the array. I can add to the array with this. So the keyword is append and my popup menu here it says new element. I'll just accept that and it is new element of string, so Mumbai. Now when I append the element to the array, you'll notice that the very end of the array, that's where the element goes. It tacks it onto the bottom, the back of it, the end of it. I can also add to the array by adding another array to it, for example. So here I'm adding an array of two values, Osaka and Karachi, to the array of cities. When I run that, you'll see now my array cities is getting quite long and there is Mumbai, then Osaka and Karachi at the end. Of course, I could view this in line as well. I can add to a specific place of the array. Now in this case, I'm telling the array cities, cities, I want to insert some information to the array called cities. What I'm inserting is in the parenthesis a string called Paris, comma, and then the location at position one. Now instead of replacing Cairo, it just pushes Cairo down to position two. So Tokyo position zero, then Paris, then Cairo. Let's remove elements from the array. This will remove the element at position four from the array. Now right now, zero, one, two, three, four. That's Mexico City, running this removes the value of Mexico City. If we take a peak, I have removed it. We can actually keep that element that we have removed for use later on like this. This time, I'm gonna remove the element of position six, but I'm storing that in the constant removedCity. It's Amarillo, and then I can easily print. We can easily remove the first and last elements. Just like this, removeFirst and removeLast. And again, I could use these values in something else, Tokyo and Karachi. I can remove everything and that will give me an empty array right here. Let's take a look at how many items are now in that array. I'll do that with a count method and it is zero 'cause I've removed it all. Sometimes you won't know the contents of an array when you first create it. Well, we did this for other types that look like this. So I'm just establishing word as a type of string. For an array, it's similar. There are a couple ways. So let's set up a new array. So here's a new array of type String which is equal to an empty array. There's a shortcut for this. It looks like this. This is an array of strings just like that. You'll probably see both of these methods. Let's practice this a little bit. I'm going to append banana to new array and then I'm going to insert orange of position one of another array. That won't work, I'm getting a terrible error because another array really has nothing in it. There is no position one. It has to be position zero to make this work at the beginning. Oh, and little syntactical error there. And then I can assign a new array called fruitArray which is equal to new array plus another array. If I add and look into the count of fruitArray, I have two elements in it and I can always loop over that. And there we go. It's printing both banana and orange. Thanks for watching, stay tuned for the next lesson where I'll show you how to use dictionaries.

Learn on the Go

Take your learning anywhere — the KnowledgeCity mobile app lets you watch lessons on the go.