‫-: Hi.
‫Within this lecture we are going to create our first project
‫in order to understand classes a little bit more.
‫Rather than going with single view app,
‫this time I'm going to go for Mac OS
‫and I'm going to create a command line tool.
‫If you remember, I showed you this technique
‫in the beginning of this course
‫before we dive into the playgrounds.
‫So if you had a problem with playground
‫there is a high probability that you have worked
‫with this now, but if you haven't, it's not an issue,
‫just create a new Mac OS line command tool
‫and you can name it musician class.
‫So that's what we are going to build in this lecture.
‫We're going to create a musician class
‫and we are going to see almost every aspect
‫of classes so that we can understand them better
‫and we can just implement them in our projects as well.
‫So this is going to run this codes inside of our Mac
‫and we are not gonna be dealing
‫with main dot storyboards or simulators.
‫So if you just run this, it'll print out the result in logs
‫so we can actually follow and track what's going
‫on inside of logs as well as you can see it printed out.
‫Hello world.
‫Can we do this in playgrounds?
‫Of course we can, but we are going to learn a lot
‫and we are going to work
‫with different kinds of view controllers.
‫We are going to see how to work
‫with multiple view controllers
‫or dot swift files in this case, not view controllers
‫so that it would make sense to work this inside
‫of command line tool.
‫So come over here and just say file new, Okay?
‫because we are going to add a new file.
‫So let's say file, okay?
‫And rather than coco touch class, I'm going to go
‫for a Swift file because I'm going to create a class
‫in the Swift while, and I'm going
‫to name my class musicians. Okay?
‫So this will be my musician class,
‫this will be my model actually.
‫So in order to create a class, it's fairly
‫easy, you can just write class, okay?
‫And then we are going to start with
‫an upper case rather than lower case.
‫Remember, in classes we use upper case like this musicians.
‫And then after you can open a curly brace
‫and it'll close it for you.
‫This is your class.
‫Right now, we actually created our class.
‫So we can have some attributes,
‫we can have some actions in this class right?
‫So let's start, what can a musician have?
‫It can have a name for example.
‫Okay, I'm gonna initialize this
‫to be an empty string first.
‫So later on it'll have an age.
‫I'm going to say this is zero
‫and it will have an instrument as well.
‫So you can make this an empty string too.
‫So suppose that we will have this musician's class, okay?
‫And it will have these three attributes.
‫So how can we create a musician object?
‫Since now we have our class, it's fairly easy.
‫We can go to our main dot swift file,
‫suppose that main dot swift is your view controller
‫and we have created a musician class, and now
‫I can create an object out of that musician class in here.
‫So first musician will be James
‫and if I write musicians, it should have show up.
‫Yeah, it sees the musicians, as you can see
‫if I do a command B, it will build up the project
‫or it can come over here to product and say build.
‫So the files will be synchronized
‫inside of our xcode project.
‫So if I write
‫let James or let something else, if I type musicians
‫as you can see now it sees the musician's class.
‫So double click on this,
‫and as you can see
‫there is a C character on the left hand side.
‫If you do a string,
‫this is kind of creating a string object
‫like I'm creating a musician's object right now.
‫So if I open parenthesis and close this, it means
‫that I'm creating this object out of the musician's class.
‫And right now my object is actually created.
‫So I managed to create an James instance
‫of this musician class.
‫So as you can see, James has the type musicians
‫and if I say dot, it can see the attributes
‫like age, instrument, and name.
‫So these are the attributes that we have given here.
‫So let me say property or attribute, okay?
‫So I can just define the age of James here.
‫So I can say 50 and I can come over here and say
‫James dot name is James Hatfield.
‫Okay? And then later on I can define
‫the instrument as well, so this will be a guitar.
‫So do this for your own favorite musician
‫and you will see it will work.
‫So let's try to print age of James.
‫Here you go. We now see the 50.
‫That's cool, right?
‫We have created a musician model and we have
‫created an object out of that model
‫and we can set the attributes right now.
‫Now suppose that we are getting
‫this information from internet.
‫We're downloading from a database
‫like Firebase, Okay?
‫So if we work on a model
‫this would be much more structural than ever before.
‫So we can actually save all those values
‫into array, different arrays
‫and we can try to synchronize these arrays using indexes.
‫But if we just create a model out of these values
‫and we can just add those model values to an array
‫it will be much more structural.
‫And we're gonna do that in the following lectures.
‫Don't worry.
‫Right now, suppose that I didn't define
‫the values as I did in the James.
‫So we have those previously defined values, right?
‫So let me try to print James dot H
‫without giving the values.
‫So what will it be?
‫It will be zero because why?
‫Because I have initialized this to be zero, right?
‫And if I do do this print James dot name
‫it'll be an empty string.
‫So what's the problem about that?
‫So maybe I don't want to initialize this.
‫Maybe I just want to say this will be an integer
‫or this will be a string because I don't want to
‫get number zero when I don't give the age value.
‫Okay? So can I do this?
‫Yes, of course I can, but it will give me an error.
‫So as you can see, class musicians has no initializers.
‫So if I create a property, if I create an attribute
‫without initializing, I will get an error.
‫But there is a solution to that problem as well.
‫We have something called initializer
‫and maybe you have heard this
‫as a constructor in other programming languages.
‫So there is a special function
‫in this musician class, in all classes actually
‫it defines what to do when we create this object
‫when we create an object out of this musicians.
‫So I can't do the initialization as well to
‫make this go away.
‫But if I don't want to do that
‫if I don't want to initialize this properties
‫then I have to create an initializer.
‫So if you write in it, as you can see
‫this is an initializer declaration
‫and this is what happens when an object is created
‫for the first time or second time.
‫It doesn't matter actually
‫when you create an object, it will happen.
‫So what exactly can we do inside of this unit?
‫We can do anything we want.
‫It doesn't have to be related with class musicians at all.
‫So let me do an example so you will understand it better.
‫So if I initialize this
‫and this and this last one,
‫the error will go away, okay?
‫And I will just print something inside of my init function.
‫So I can say musician created for example.
‫So whenever I create an object, it will print this lock.
‫So let me create this, James.
‫As you can see, this is only creating the James
‫and this will only print out the James dot name
‫but before it prints, it says musician created.
‫So whenever I create another instance
‫of this musician class, it'll again print musician created.
‫So it isn't related with properties or attributes at all.
‫Of course we gonna make it related.
‫But let me do this example as well.
‫So I'm creating another instance from these musicians.
‫And if I run this you will see there will
‫be two musician created lock.
‫So in init you can write what you wanna do when
‫an object is created.
‫So this is Initializer, okay?
‫And again, you may have heard this
‫as a constructor in other programming languages.
‫What we actually wanna do in here is to
‫get some name agent instrument property values
‫and assign them as our properties, as our attributes.
‫We are gonna get those values
‫from the developer in Main dot Swift.
‫So let's stop here and do that within the next lecture.