Leave a rating/review
Notes: 34. Introduction
Update Notes: The student materials have been reviewed and are updated as of October 2021.
Hey everyone! I’m Chris, and welcome to the final part of this course, Functions and Named Types. So far in this course, you’ve used the Types that Swift gives you. In this part of the course you’ll be combining much of what you’ve learned to create your own types. Before we get started, we need to go over some terminology.
First, you’ve heard the term “type” to describe things like Ints and Strings. There are actually two kinds of types: Named Types & Compound Types. A Named Type is a type that has a name when it’s defined.
You’ll learn about two Named Types in this part: Structures & Classes. Most of the types you already know are also Named Types! Ints, Strings, Booleans, and even Arrays, are all named types.
So, Named types have names but Compound Types do not. Compound Types are defined by the other types they contain. They can bean be made up of named types, or even other compound types. Instead of a name, a compound type is expressed by a Type Signature.
To show you an example, take a look at these Tuples! Tuples are a compound type. Tuples can be made of two Ints, or a String & a Bool, or a Double and a tuple of two Ints.
You’ll learn about one more Compound Type in this part: Functions You wrote some of these in Bullseye, but you called them Methods. I’ll talk about why.
Types can described in another important way: by how they’re data is stored and copied. All of the types you’ve been working with in this course are Value Types.
When you assign the value of one instance of a value type, to another instance of that type, those two instances are completely independent.
Strings are value types, so if you make a copy of this name, and set the value of the copy to something completely different, those two names will no longer match. Structures and Tuples are value types.
Reference types work differently. If you make a copy of a value type, the two instances are both referencing the same data. Making a change to one instance will change both instances. Classes and Functions are reference types.
I’ll be covering a lot of new material in this part.
You’ll start out with a solid introduction to functions. Functions, like the methods you saw in Bullseye, provide functionality to your app. They also help you reuse code.
Next you’ll write a basic Structure and learn about its parts: Properties, which are what a structure “has”, and methods: what a structure “does”.
And you’ll finish up this part and the course with an introduction to Classes. Classes are very similar to Structures. They also have properties and methods! But one big difference is how they copy values, so you’ll revisit the idea of Value and Reference types here.
I’ll still have a few challenges for you along the way to help you check your knowledge and get some great practice. When you’re ready, let’s take a look at Functions.