Check out a free preview of the full iOS App Development with Swift course

The "Custom Types" Lesson is part of the full, iOS App Development with Swift course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano walks through defining custom types and classes, briefly discusses structures, enums, and protocols, and demonstrates using the self keyword in place of JavaScript's this keyword. Swift has full support for OOP, and classes can be extended.

Preview
Close

Transcript from the "Custom Types" Lesson

[00:00:00]
>> Okay, custom types, now we're going to go faster in this part. And we're going to focus only on the parts that we will actually spend time. So we have typealias. Typealias is a way to actually define a different name for another type. That can add some semantics, for example, coordinate, it's a tuple of integer, I can create a type alias of a distance that is actually a float.

[00:00:29]
And then we can express every time you see a distance is in meters, or miles. So you set that and then instead of just saying float, you're using distance as a type. You have an enums, okay? Like in other languages, like it gets more complicated, enums can have a type can have a low value, but we're not going to use this.

[00:00:52]
So I'm just focusing on the import around the basics. We can create classes. We can create the structures. What's a structure? The structure at first it looks very similar to tuples, okay? With names with one difference. So we can actually quickly, Add methods. So in a structure I can have methods.

[00:01:21]
I can have initializers, those are kinds of constructors. And what's the difference with a class? We don't use inheritance, so we cannot have a superclass with a struct, it's just data with methods. And every object instance of a structure, typically we call them instances. Every instance of a structure is passed by value, by copy.

[00:01:51]
Every instance of a class is passed by reference, okay? We will see a couple of differences later on that. And finally we have protocols, the protocol, let me give you first a quick definition, quick dirty and wrong definition. It's like an interface in Java, just for you to get into some mental model there.

[00:02:18]
An interface in Java or TypeScript or C# or PHP, if you want. So an interface is a set of function names that then you need to comply with, something like that. But it's wrong, as I mentioned before, because here, the protocol is different. You can do that if you want, but also there are a couple of more examples here.

[00:02:37]
But a protocol can have optional functions. So then you're not forced to implement them. So you can, if you want, but you're not forced to. And you can even create the protocol without the function, that is just a flyer. It's a way to categorize objects. And you can apply protocols to enum, to classes and structs, to every data type kind of, every custom data type, okay?

[00:03:14]
We will see all of this in action, okay? So if you wanna play with this, class is actually pretty simple. You can create hands class human, let's say that every human has a name. If you wanna set an initializer basic constructor, it's called init Without the func keyword.

[00:03:40]
If you wanna have methods, it just the func keyword, like on other languages. If you want initializers with arguments, you just create an init with arguments such as a need with name. And we typically use this with because it's part of the name, okay? So then I can say name=withname, but you can say withname, that's a weird name for a variable.

[00:04:10]
So it's good from the outside, but remember that you can separate the argument labels from the actual variable names with a space. So I can call it name, they have ambiguity. How do you solve the problem in Java or JavaScript, here? Any language, anyone? This, well, here in Swift, it's not this, it's self.

[00:04:42]
You'll get used to that, okay? It's self. So self.name = name. Well, this is empty, that's why it's complaining. That's how you quickly create a class. And then it's OOP, so I can create, that's a human, so we can create a customer. And we can say that we are extending from human with column.

[00:05:11]
It is similar to C# or C++, we don't use the extends keyword, we just use colon. But actually, we are not going to write a lot of OOP code in our Swift UI application. A struct is just the same. So instead of class, I say struct. I cannot use, I don't use inheritance in this case, so I just created a structure.

[00:05:41]
I can have methods, I can have initializers. The only difference that we have, with struct, actually very interesting difference. Do you remember what happened with classes if I set a variable with the property that is not optional? I have an error, let's do that. Saying hey, you must set a string or create an initializer that sets that string because it's mandatory, it's not optional.

[00:06:15]
If it's not optional, I cannot create objects of this class without a name. Well, on structures I don't have that error because the structure creates an automatic initializer. So I have an initializer with a name, by default. So if I wanna create a human let's say that I'm a human kind of.

[00:06:43]
I don't need to create an explicit initializer receiving the name, it's already built in. So every time you don't have OOP, you have just data and a structure in memory, you just need to create a structure. If you're not using OOP or data sheets just create a structure.

[00:07:13]
It's faster, it uses less memory than classes, yep?
>> [INAUDIBLE].
>> When you pass a human of if I receive a function that will print a human, so I receive a human, okay? And I do something with that. So if I call print on a pass max as an argument, it's creating a copy.

[00:07:38]
If it's a class, it's passing it by reference. In this case, it's creating a copy. And I know that sounds kind of scary, it's like, I don't wanna make copies of my objects all the time. But this also had to do with memory management, and the stack memory, and so on.

[00:07:59]
And this is actually more efficient. Structures, even if we are cloning this, the data, it's just basic data. It's being done really fast and optimized in Apple platforms, for example. So using a structure is much faster than using classes.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now