iOS App Development with Swift

Custom Types

Maximiliano Firtman

Maximiliano Firtman

Independent Consultant
iOS App Development with Swift

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.


Transcript from the "Custom Types" Lesson

>> 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.

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.

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.

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.

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.

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.

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?

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.

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.

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.

You'll get used to that, okay? It's self. So = 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.

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.

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.

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.

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.

It's faster, it uses less memory than classes, yep?
>> 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.

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.

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