image
Details

Contents

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

The course is part of this learning path

Start course
Difficulty
Intermediate
Duration
1h 22m
Students
24
Ratings
5/5
starstarstarstarstar
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 finalize our app by displaying the image name and the job of the chosen Simpson in the details View Controller. So, we already have our views defined in here, like image view, name label, job label. We're going to need a Simpson object in this class, right?

And the Simpson object will come from the main View Controller. So, we can easily create our Simpson object right now in here and we can try to display the related values in related views. So, let's say this is going to be a selectedSimpson and this will be a Simpson type. I will later on get this value from viewcontroller.swift using prepare for segue function as we did before. And in here, I'm going to go over and say that nameLabel.text is going to be selectedSimpson.name. So, this is going to take the selectedSimpson.name. So, if I do 'Ctrl+B', it will give me an error saying that this has no initializer and I cannot go for an initializing kind of thing  in here like this because it will ask me for some parameters, right? So, I cannot construct a Simpson object in here simply because I don't know the values, I cannot do it like this without giving any parameters. So, this is kind of tricky. Rather than doing this, I'm going to go over and say this is going to be a Simpson type but it's going to be optional. At this point, I don't know whether it's going to be here or not but in real life, of course, it's going to be there because I'm going to pass that information from View Controller. And if I do this optional, this won't be an issue unless I give an optional value to here as well. So, rather than saying nameLabel.text  = selectedSimpson.name, I'm going to say selectedSimpsonOptional.name. And since I'm going to pass this information as I said before, this won't create any issue for me, and this is much safer. So again, jobLabel.text is selectedSimpsonOptional.job and image view, again, so imageView.image so this is going to be selectedSimpson.image. These are all optionals. So, that's great. We're done. And as you can see when you work with a model, when you work with a class, it's much clear what you're trying to do, right? We are displaying the name, job, and image in here, and it's much more structural, much more clear code. So, how are we going to get the selectedSimpson be equal to this chosen Simpson in the tableView in the ViewController.swift? The same way as before, we're going to use prepare for segue, we're going to use perform segue, and we're going to use the old segue related functions, okay? So, first of all, let me call this didSelectRow, okay? Remember this is the function that gets called when somebody clicks one of the items on the tableView. So, I'm going to create a segue here, rather not create actually, I'm going to perform a segue here. So, say "toDetailsVC". So, that was our segue identifier, right? Let me check. So, let me go over here, toDetailsVC. This has to be exactly the same so make sure that you're writing the same thing. And here I'm going to use nil sender... And before we do that, of course, once somebody clicks one of the items, we're going to make sure that we're getting that number. We're getting the index of the clickedSimpson, chosenSimpson and we're assigning this to be a variable so that we can use it on our prepare for segue function. So, I'm going to say chosenSimpson, mySimpsons and indexPath.row. So, this will give me the selectedSimpson over here, right? So, I can come over to my prepare for segue function and use this variable. So, that's it for didSelectRow and let's go over here to another function to create another function and they prepare for segue. So, what do we do here? First, we check if the segue that we're talking about is the same segue that we want to commit all of these operations. So, I'm going to say if segue.identifier is really the toDetailsVC. And in this app again, we only have one segue but it's kind of important to get this habit of checking this identifier because you're going to have a lot of segues in your apps later on. So, we're going to have to take this chosenSimpson and pass this information to our second View Ccontroller. Again, in order to do that, we cannot define those Simpson in here because we want to take the selectedSimpson and make this equal to chosenSimpson. So, let's adjust this a little bit. Let's first create the destination View Controller, okay? So, destinationVC is going to be segue.destination, and remember we have to first cast this as detailsVC, okay? And now if I do command B, I can reach the detailsVC's attributes and properties. So, let me go over here and do command B and say detailsVC or destinationVC, sorry, destinationVC.imageView, jobLabel view, we have seen all the views here and what we want is the selectedSimpson. I'm going to make this equal to chosenSimpson. And as I said before, we cannot do that over here, we have to define this in our class but outside of this function because I cannot simply come over here and say chosenSimpson. It is not in the scope, we have gone over this several times. So, let me delete this and go over to the top here, let me create a new variable called chosenSimpson and this is going to be a Simpson and again, if I open this parentheses it will ask me for some parameters. I don't want that, I'm going to do the exact same thing as before. I'm going to say that this is going to be a Simpson type, okay? It will give me an error so I'm going to make this optional. So, I have a chosenSimpson of variable and it's an optional Simpson type. So, here as you can see now I have this, I can go over here and say chosenSimpson, mySimpsons array indexPath.row. So, this is now okay, it should be working once I come over here as a chosenSimpson. That's great. Now we can test this actually. We're done with our app. We have written so many codes during this building process and let's test them all. So, click on 'Homer.' Here we go. Homer seems to Nuclear Safety. Let's go for Marge; Marge Simpson house wife. So, let's go for Bart; Bart Simpson's student. So, this looks good, right? Lisa, student and let's test for Maggie. Maggie Simpson baby. So, this is great. This is what we had in mind when we first started this project. Again, you have learned a lot during this section. You have learned what it feels to be working with object-oriented programming with classes, with models. So, you have created your own class in which you define your own attributes with some initializers and you created your Simpson objects, you set up your TableView, you connected those arrays with tableViews and you displayed all the related information in a structural manner. So far so good, I hope you enjoyed this section. So, we're going to stop here and we're going to continue within the next section.

About the Author
Students
1649
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