KnowledgeCity

Swift Beginner: Classes and Structures

As you begin building more sophisticated projects, you’ll want to create your own types that include properties and other logic.

As you begin building more sophisticated projects, you’ll want to create your own types that include properties and other logic. In this module, you’ll learn about two of those: classes and structures.

We use structures in Swift fairly often. You’ll learn how to build your own structs with your own properties and then how to create instances of those structs to add to this flexibility. Swift is also an object-oriented programming language, so you’ll learn about organizing your code into classes and subclasses and how to initialize your properties in both of them. You’ll also learn the basics of subclass and superclass inheritance, which means how properties in a child class can inherit the traits of those properties from the parent class. Finally, we’ll explore property observers, which are chunks of code that keep watching for changes that may happen within your struct and then act accordingly, depending on the logic you have implemented. 

Learning Objectives

  • Understand creating new types
  • Recognize structures and classes
  • Identify object-oriented inheritance
  • Explain the value and reference types

Author: Mark Nair

Duration: 40m · 6 lessons
Level: Beginner
Language: English

Skills you’ll gain

Abstract ClassApplication Foundation ClassesCode StructureData StructuresObject-Oriented Programming (OOP)Structured Programming

What You'll Learn

  • Create your own custom types using structures and classes
  • Build structs with their own properties and create instances of them
  • Initialize properties in both structures and classes using initializers
  • Organize code into classes and subclasses using object-oriented programming
  • Apply subclass and superclass inheritance so child classes inherit parent traits
  • Use property observers to watch for and respond to changes within a struct

Key Takeaways

  • Swift lets you create your own types that include properties and other logic, such as classes and structures.
  • Structures are used in Swift fairly often, and you can build your own structs with custom properties and create instances of them.
  • Swift is an object-oriented programming language, so code can be organized into classes and subclasses with initialized properties.
  • Subclass and superclass inheritance means properties in a child class can inherit the traits of those properties from the parent class.
  • Property observers are chunks of code that watch for changes within your struct and act accordingly based on the logic you implement.

Frequently Asked Questions

What does this course cover?

This module covers creating your own custom types in Swift, specifically classes and structures. It includes making structs, creating instances, initializers, property observers, classes, and inheritance.

Who is this course for?

It is aimed at those beginning to build more sophisticated Swift projects who want to create their own types that include properties and other logic.

What skills will I gain from this course?

You will gain skills in object-oriented programming (OOP), code structure, data structures, structured programming, abstract classes, and application foundation classes.

What is inheritance in this course?

Inheritance covers the basics of subclass and superclass inheritance, meaning how properties in a child class can inherit the traits of those properties from the parent class.

What are property observers?

Property observers are chunks of code that keep watching for changes that may happen within your struct and then act accordingly, depending on the logic you have implemented.

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 classes and structures, including making structures, instances, initializers, property observers, classes, and inheritance. Structures or structs, as what we say, are very common in Swift. We use them when we need to create an entirely new type of data structure for our work. I'm gonna quote the Swift programming language guide on this one, just to clarify it. To quote: "Structures are general-purpose, flexible constructs that become the building blocks of your program's code. You define properties and methods to add functionality to your structures and classes using the same syntax you use to define constants, variables, and functions." Well, that all sounds pretty good, but what does it mean? Well, let's take a look. I need a new type of information, new type of data for a bicycle. Now, early on, we would do something like this: "let bicycle =" And I'll just say, "Road bike." But that's all we have, there's nothing else about Road bike in here. If I wanted more information about Road bike, I could say Road bike. But it's just a string. So, the limitation here is it's a descriptor, a paragraph, words that describe Road bike, er, describe bicycle, but it doesn't let us do anything with it. A data structure does. So let's take bicycle and make this into a data structure. The way we do this is we begin with the keyword struct, which stands for structure. Now, I'm gonna make my struct called Bicycle. "Bicycle." Now, the way we do this is we capitalize the struct name right here. That's Bicycle. So the keyword struct, capitalize B for Bicycle. Then we add with the curly brace. Now, that's the skeletal view of a struct. So right now I have a struct called Bicycle. There's nothing in it. So let's give it some properties. Now, properties are things like this. I'll say "var name." And that is of type String. I don't have a name yet for the Bicycle. So I'm just going to set the placeholder of name without declaring the type. Like this. Swift will complain saying, "I don't know what to do about name. Name could be all sorts of stuff." That's why the error message is Type annotation missing in pattern there. Let's give it a type. It'll be a String. Let's make a brand. Let's give it some PSI for the tires. I'll make this a type of Int. Let's give it a weight. I'll make that a type of Double so we can have some 59.355 or something like that. 59 it's heavy. Okay. And let's give it a boolean value. Is it comfortable? It's looking pretty good. This is just the basic structure of a Bicycle. And the Bicycle now has these different properties, name, brand, tirePSI, weight, comfyRide, that we can then change, access, update, you name it. Now, how we're going to do that is by making an instance of this struct of Bicycle. Thanks for watching. In the next lesson, you'll learn about making instances of your struct.

Learn on the Go

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