1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Using Object-Oriented Programming in Swift

Creating a Model

Contents

keyboard_tab
Object-Oriented Programming
2
What is OOP?
PREVIEW6m 49s
3
11
Details
9m 33s

The course is part of this learning path

Start course
Overview
Difficulty
Intermediate
Duration
1h 22m
Students
7
Description

This course delves into Object-Oriented Programming (OOP), covering its main components, and then putting these into practice by walking you through the creation of an app using OOP.

Learning Objectives

  • Learn about classes and how you can leverage them in your code.
  • Learn concepts such as Enum, Inheritance, and Access Levels
  • Understand how these can be used to build apps

Intended Audience

This course is intended for beginners who want to learn how to build apps using Swift.

Prerequisites

To the most out of this course, you should have some basic understanding of programming and computer science in general.

 

Transcript

Hi. Within this lecture, we're going to write our Simpson class. We're going to create a Simpson model in which we will have all the information that we need regarding to those Simpsons. So, in order to do that, of course, I'm going to create a new.swift file and I'm going to write my class inside of that swift file, so that we can create some objects from that model and we can display them in our table view eventually. So, you know how to do that, right? Maybe you can try to do it on your own. This is going to be exactly the same thing that we have done with the musician class. So, I suggest you pause the video and try to create your own Simpson class. I hope you managed to make it. So, let's do it together. I'm going to create a swift file over here, and I'm going to name it Simpson. So, that's it. So, this is going to be exactly the same as before. I'm going to go for class and this will be a class of Simpson and open a curly braces in which I will have some properties. So, first property will be name. So, I'm going to initialize this to be an empty string and then a job, and we're going to have an image. So, this is going to be a UIImage as before. So, maybe you got stuck here somehow because we cannot call this UIImage inside of our class, because we don't have the necessary library in order to call this. We just have this import Foundation in here and this won't do the work. Rather than importing the Foundation, we have to import the UIKit. Maybe you have understood that you have to import this library. If so, congratulations. As you can see, I can call the UI image over here. So, rather than saying just import Foundation, I'm going to have to import the UIKit here as well in order to call this. So, again, we have some different libraries. We don't call everything at once. So, if we need something like that, we can import whenever we need it. So, next thing, what do we need? We need to create those homer object or other objects. So, let me write Simpson class or Simpson Objects in the viewDidLoad. We can come over here and we can create a Simpson object and we can actually make it work by giving all the values that we want. So, for example, I can say let homer and this is going to be a Simpson object, and I can go over and say homer.name is Homer. And we can go for homer.job, I believe his job is Nuclear Safety Inspector or something. And the homer image, UIImage named. Let me open that parenthesis, find named, and the name of that image is homer. So, this is asking for a string. So, this is homer and this will be fine. And this is actually wanting us to force unwrap this and that's okay because we know homer.PNG or homer.JPG actually exists in our Assets folder. So, we are certain that this is going to be working. And this is fine. We can actually do that, but I don't want to do that this way. This will be too difficult for me to do this for every object. Instead, I just want to go over and create those without initializing the values and I'm just going to assign those values in a constructor. So, name will be string, job will be string, image will be a UI image. Why I'm doing that?

In case of missing an age or in case of forgetting a value, so this is much more safer. Remember how we do that. We can use a special function called init. So, this is an initializer declaration and inside of this, I can write whatever I want to do when an object is created out of this class. So, I'm going to ask for a simpsonName and this will be a String. And a simpsonJob, this will be again a String and a simpsonImage, this will be a UIImage. So, I can come over here and say name is simpsonName. Job is simpsonJob, and the image is also a simpsonImage. So, this is a simpsonImage. So, let me do a command B and see if there's something wrong here, I miss about the image. So, this is working right now. I can create a new object from the Simpson class using this initializer, and I believe this is much more better than what we have done previously. So, we're going to use this class and this will be happening whenever I create a new object of Simpson class. So, let's go ahead and try this. So, let me go to view controller and let me delete everything in here because we're going to do the same thing. But right now this will give me an error if I do command B as you can see. And this won't even make sense. So, let me write it from the beginning. So, Simpson open parentheses and it will ask me for some parameters. Parameters that we have defined as an initializer. So, for name, I'm going to go for Homer Simpson. For job, nuclear safety, and simpsonImage is going to be a UIImage, open parentheses, find the named and just say Homer here, and with an exclamation point, I believe in here and this is going to be okay. This is much more safer. In the previous way, we could have just forgotten about the simpsonJob for example, and this will bring us some problems. So, here, this is giving me some error but Simpson name is already there, so I'm going to go for a command B and now this is okay. So, this is giving me some errors saying that you have created Homer but you haven't used it. We're going to use it. Don't worry. So, let's go and copy and paste this for a few times because we're going to create other objects as well. So, let's do this fast. This is going to be Marge. Marge Simpson. Simpson job, I believe she's not working. So, she's going to be a housewife. If not, my bad. I really don't know. So, this is Marge. And let's go for Bart. So, Bart Simpson, simpsonJob is student. UIImage name is Bart. So, let me go to fourth one. So, this is going to be Lisa. And Lisa Simpson, simpsonJob, she's student as well and much better than Bart as far as I remember. So, let's go for Maggie. And simpsonName is Maggie. So, her name was Margaret, Maggie. And Job, let's say baby. And the UIImage is Maggie. So, that's all. That's all we have to do. We have created our model objects. And let's check the names. Homer, Lisa, Maggie, Marge. So, we're getting some warnings saying that you have created all of these objects, but you haven't actually used them. We're going to use them. Let's create a homerArray in which we will have these homer objects. So, you can create array like this and just say Homer, Marge, Bart, Lisa, and Maggie. So, this will be an array of Homer Simpson, not Homer Simpson, but Simpson objects. We're going to use leverage disarray in order to display those values in our table view. So, let's do that within the next lecture.

 

About the Author
Students
611
Courses
55
Learning Paths
3

Atil is an instructor at Bogazici University, where he graduated back in 2010. He is also co-founder of Academy Club, which provides training, and Pera Games, which operates in the mobile gaming industry.

Covered Topics