1 00:00:00,600 --> 00:00:01,433 ‫-: Hi. 2 00:00:01,433 --> 00:00:05,340 ‫Within this lecture we are going to create our first project 3 00:00:05,340 --> 00:00:09,120 ‫in order to understand classes a little bit more. 4 00:00:09,120 --> 00:00:11,700 ‫Rather than going with single view app, 5 00:00:11,700 --> 00:00:14,490 ‫this time I'm going to go for Mac OS 6 00:00:14,490 --> 00:00:17,580 ‫and I'm going to create a command line tool. 7 00:00:17,580 --> 00:00:20,790 ‫If you remember, I showed you this technique 8 00:00:20,790 --> 00:00:22,800 ‫in the beginning of this course 9 00:00:22,800 --> 00:00:25,560 ‫before we dive into the playgrounds. 10 00:00:25,560 --> 00:00:28,320 ‫So if you had a problem with playground 11 00:00:28,320 --> 00:00:31,590 ‫there is a high probability that you have worked 12 00:00:31,590 --> 00:00:36,420 ‫with this now, but if you haven't, it's not an issue, 13 00:00:36,420 --> 00:00:39,630 ‫just create a new Mac OS line command tool 14 00:00:39,630 --> 00:00:42,540 ‫and you can name it musician class. 15 00:00:42,540 --> 00:00:45,060 ‫So that's what we are going to build in this lecture. 16 00:00:45,060 --> 00:00:47,682 ‫We're going to create a musician class 17 00:00:47,682 --> 00:00:51,150 ‫and we are going to see almost every aspect 18 00:00:51,150 --> 00:00:54,810 ‫of classes so that we can understand them better 19 00:00:54,810 --> 00:00:59,430 ‫and we can just implement them in our projects as well. 20 00:00:59,430 --> 00:01:04,260 ‫So this is going to run this codes inside of our Mac 21 00:01:04,260 --> 00:01:06,360 ‫and we are not gonna be dealing 22 00:01:06,360 --> 00:01:09,360 ‫with main dot storyboards or simulators. 23 00:01:09,360 --> 00:01:14,360 ‫So if you just run this, it'll print out the result in logs 24 00:01:15,030 --> 00:01:18,360 ‫so we can actually follow and track what's going 25 00:01:18,360 --> 00:01:22,530 ‫on inside of logs as well as you can see it printed out. 26 00:01:22,530 --> 00:01:23,670 ‫Hello world. 27 00:01:23,670 --> 00:01:25,890 ‫Can we do this in playgrounds? 28 00:01:25,890 --> 00:01:30,000 ‫Of course we can, but we are going to learn a lot 29 00:01:30,000 --> 00:01:31,110 ‫and we are going to work 30 00:01:31,110 --> 00:01:34,170 ‫with different kinds of view controllers. 31 00:01:34,170 --> 00:01:35,640 ‫We are going to see how to work 32 00:01:35,640 --> 00:01:38,250 ‫with multiple view controllers 33 00:01:38,250 --> 00:01:41,790 ‫or dot swift files in this case, not view controllers 34 00:01:41,790 --> 00:01:45,300 ‫so that it would make sense to work this inside 35 00:01:45,300 --> 00:01:47,340 ‫of command line tool. 36 00:01:47,340 --> 00:01:51,390 ‫So come over here and just say file new, Okay? 37 00:01:51,390 --> 00:01:54,450 ‫because we are going to add a new file. 38 00:01:54,450 --> 00:01:57,210 ‫So let's say file, okay? 39 00:01:57,210 --> 00:01:59,730 ‫And rather than coco touch class, I'm going to go 40 00:01:59,730 --> 00:02:04,050 ‫for a Swift file because I'm going to create a class 41 00:02:04,050 --> 00:02:06,510 ‫in the Swift while, and I'm going 42 00:02:06,510 --> 00:02:10,290 ‫to name my class musicians. Okay? 43 00:02:10,290 --> 00:02:12,450 ‫So this will be my musician class, 44 00:02:12,450 --> 00:02:16,380 ‫this will be my model actually. 45 00:02:16,380 --> 00:02:20,220 ‫So in order to create a class, it's fairly 46 00:02:20,220 --> 00:02:23,610 ‫easy, you can just write class, okay? 47 00:02:23,610 --> 00:02:26,520 ‫And then we are going to start with 48 00:02:26,520 --> 00:02:29,130 ‫an upper case rather than lower case. 49 00:02:29,130 --> 00:02:34,130 ‫Remember, in classes we use upper case like this musicians. 50 00:02:34,710 --> 00:02:37,620 ‫And then after you can open a curly brace 51 00:02:37,620 --> 00:02:39,420 ‫and it'll close it for you. 52 00:02:39,420 --> 00:02:40,980 ‫This is your class. 53 00:02:40,980 --> 00:02:44,193 ‫Right now, we actually created our class. 54 00:02:45,090 --> 00:02:47,340 ‫So we can have some attributes, 55 00:02:47,340 --> 00:02:50,010 ‫we can have some actions in this class right? 56 00:02:50,010 --> 00:02:54,450 ‫So let's start, what can a musician have? 57 00:02:54,450 --> 00:02:57,000 ‫It can have a name for example. 58 00:02:57,000 --> 00:02:59,040 ‫Okay, I'm gonna initialize this 59 00:02:59,040 --> 00:03:01,350 ‫to be an empty string first. 60 00:03:01,350 --> 00:03:03,990 ‫So later on it'll have an age. 61 00:03:03,990 --> 00:03:06,510 ‫I'm going to say this is zero 62 00:03:06,510 --> 00:03:09,930 ‫and it will have an instrument as well. 63 00:03:09,930 --> 00:03:14,280 ‫So you can make this an empty string too. 64 00:03:14,280 --> 00:03:18,660 ‫So suppose that we will have this musician's class, okay? 65 00:03:18,660 --> 00:03:21,270 ‫And it will have these three attributes. 66 00:03:21,270 --> 00:03:24,210 ‫So how can we create a musician object? 67 00:03:24,210 --> 00:03:28,440 ‫Since now we have our class, it's fairly easy. 68 00:03:28,440 --> 00:03:31,770 ‫We can go to our main dot swift file, 69 00:03:31,770 --> 00:03:35,610 ‫suppose that main dot swift is your view controller 70 00:03:35,610 --> 00:03:40,320 ‫and we have created a musician class, and now 71 00:03:40,320 --> 00:03:44,670 ‫I can create an object out of that musician class in here. 72 00:03:44,670 --> 00:03:47,070 ‫So first musician will be James 73 00:03:47,070 --> 00:03:51,150 ‫and if I write musicians, it should have show up. 74 00:03:51,150 --> 00:03:55,560 ‫Yeah, it sees the musicians, as you can see 75 00:03:55,560 --> 00:03:59,550 ‫if I do a command B, it will build up the project 76 00:03:59,550 --> 00:04:02,670 ‫or it can come over here to product and say build. 77 00:04:02,670 --> 00:04:04,860 ‫So the files will be synchronized 78 00:04:04,860 --> 00:04:07,200 ‫inside of our xcode project. 79 00:04:07,200 --> 00:04:08,220 ‫So if I write 80 00:04:08,220 --> 00:04:13,220 ‫let James or let something else, if I type musicians 81 00:04:13,860 --> 00:04:18,240 ‫as you can see now it sees the musician's class. 82 00:04:18,240 --> 00:04:20,760 ‫So double click on this, 83 00:04:20,760 --> 00:04:21,810 ‫and as you can see 84 00:04:21,810 --> 00:04:25,290 ‫there is a C character on the left hand side. 85 00:04:25,290 --> 00:04:27,180 ‫If you do a string, 86 00:04:27,180 --> 00:04:30,300 ‫this is kind of creating a string object 87 00:04:30,300 --> 00:04:34,980 ‫like I'm creating a musician's object right now. 88 00:04:34,980 --> 00:04:39,150 ‫So if I open parenthesis and close this, it means 89 00:04:39,150 --> 00:04:42,900 ‫that I'm creating this object out of the musician's class. 90 00:04:42,900 --> 00:04:47,820 ‫And right now my object is actually created. 91 00:04:47,820 --> 00:04:52,260 ‫So I managed to create an James instance 92 00:04:52,260 --> 00:04:54,360 ‫of this musician class. 93 00:04:54,360 --> 00:04:57,120 ‫So as you can see, James has the type musicians 94 00:04:57,120 --> 00:05:00,750 ‫and if I say dot, it can see the attributes 95 00:05:00,750 --> 00:05:03,990 ‫like age, instrument, and name. 96 00:05:03,990 --> 00:05:07,140 ‫So these are the attributes that we have given here. 97 00:05:07,140 --> 00:05:11,070 ‫So let me say property or attribute, okay? 98 00:05:11,070 --> 00:05:14,670 ‫So I can just define the age of James here. 99 00:05:14,670 --> 00:05:18,840 ‫So I can say 50 and I can come over here and say 100 00:05:18,840 --> 00:05:21,870 ‫James dot name is James Hatfield. 101 00:05:21,870 --> 00:05:26,010 ‫Okay? And then later on I can define 102 00:05:26,010 --> 00:05:29,613 ‫the instrument as well, so this will be a guitar. 103 00:05:30,450 --> 00:05:33,990 ‫So do this for your own favorite musician 104 00:05:33,990 --> 00:05:36,150 ‫and you will see it will work. 105 00:05:36,150 --> 00:05:40,470 ‫So let's try to print age of James. 106 00:05:40,470 --> 00:05:43,560 ‫Here you go. We now see the 50. 107 00:05:43,560 --> 00:05:45,030 ‫That's cool, right? 108 00:05:45,030 --> 00:05:48,060 ‫We have created a musician model and we have 109 00:05:48,060 --> 00:05:50,850 ‫created an object out of that model 110 00:05:50,850 --> 00:05:53,433 ‫and we can set the attributes right now. 111 00:05:54,960 --> 00:05:56,940 ‫Now suppose that we are getting 112 00:05:56,940 --> 00:05:58,770 ‫this information from internet. 113 00:05:58,770 --> 00:06:00,750 ‫We're downloading from a database 114 00:06:00,750 --> 00:06:03,210 ‫like Firebase, Okay? 115 00:06:03,210 --> 00:06:05,490 ‫So if we work on a model 116 00:06:05,490 --> 00:06:09,870 ‫this would be much more structural than ever before. 117 00:06:09,870 --> 00:06:13,410 ‫So we can actually save all those values 118 00:06:13,410 --> 00:06:15,720 ‫into array, different arrays 119 00:06:15,720 --> 00:06:19,980 ‫and we can try to synchronize these arrays using indexes. 120 00:06:19,980 --> 00:06:23,790 ‫But if we just create a model out of these values 121 00:06:23,790 --> 00:06:28,790 ‫and we can just add those model values to an array 122 00:06:28,830 --> 00:06:31,410 ‫it will be much more structural. 123 00:06:31,410 --> 00:06:33,600 ‫And we're gonna do that in the following lectures. 124 00:06:33,600 --> 00:06:34,920 ‫Don't worry. 125 00:06:34,920 --> 00:06:39,720 ‫Right now, suppose that I didn't define 126 00:06:39,720 --> 00:06:43,260 ‫the values as I did in the James. 127 00:06:43,260 --> 00:06:47,820 ‫So we have those previously defined values, right? 128 00:06:47,820 --> 00:06:51,240 ‫So let me try to print James dot H 129 00:06:51,240 --> 00:06:53,460 ‫without giving the values. 130 00:06:53,460 --> 00:06:54,960 ‫So what will it be? 131 00:06:54,960 --> 00:06:57,240 ‫It will be zero because why? 132 00:06:57,240 --> 00:07:00,960 ‫Because I have initialized this to be zero, right? 133 00:07:00,960 --> 00:07:04,360 ‫And if I do do this print James dot name 134 00:07:04,360 --> 00:07:07,020 ‫it'll be an empty string. 135 00:07:07,020 --> 00:07:10,560 ‫So what's the problem about that? 136 00:07:10,560 --> 00:07:13,470 ‫So maybe I don't want to initialize this. 137 00:07:13,470 --> 00:07:16,560 ‫Maybe I just want to say this will be an integer 138 00:07:16,560 --> 00:07:20,730 ‫or this will be a string because I don't want to 139 00:07:20,730 --> 00:07:25,320 ‫get number zero when I don't give the age value. 140 00:07:25,320 --> 00:07:28,713 ‫Okay? So can I do this? 141 00:07:29,970 --> 00:07:34,230 ‫Yes, of course I can, but it will give me an error. 142 00:07:34,230 --> 00:07:39,120 ‫So as you can see, class musicians has no initializers. 143 00:07:39,120 --> 00:07:43,320 ‫So if I create a property, if I create an attribute 144 00:07:43,320 --> 00:07:46,620 ‫without initializing, I will get an error. 145 00:07:46,620 --> 00:07:50,700 ‫But there is a solution to that problem as well. 146 00:07:50,700 --> 00:07:53,790 ‫We have something called initializer 147 00:07:53,790 --> 00:07:55,590 ‫and maybe you have heard this 148 00:07:55,590 --> 00:07:59,670 ‫as a constructor in other programming languages. 149 00:07:59,670 --> 00:08:01,419 ‫So there is a special function 150 00:08:01,419 --> 00:08:05,670 ‫in this musician class, in all classes actually 151 00:08:05,670 --> 00:08:10,380 ‫it defines what to do when we create this object 152 00:08:10,380 --> 00:08:14,700 ‫when we create an object out of this musicians. 153 00:08:14,700 --> 00:08:18,030 ‫So I can't do the initialization as well to 154 00:08:18,030 --> 00:08:19,050 ‫make this go away. 155 00:08:19,050 --> 00:08:20,970 ‫But if I don't want to do that 156 00:08:20,970 --> 00:08:24,270 ‫if I don't want to initialize this properties 157 00:08:24,270 --> 00:08:27,480 ‫then I have to create an initializer. 158 00:08:27,480 --> 00:08:30,000 ‫So if you write in it, as you can see 159 00:08:30,000 --> 00:08:32,640 ‫this is an initializer declaration 160 00:08:32,640 --> 00:08:37,140 ‫and this is what happens when an object is created 161 00:08:37,140 --> 00:08:39,180 ‫for the first time or second time. 162 00:08:39,180 --> 00:08:40,380 ‫It doesn't matter actually 163 00:08:40,380 --> 00:08:43,443 ‫when you create an object, it will happen. 164 00:08:44,610 --> 00:08:48,270 ‫So what exactly can we do inside of this unit? 165 00:08:48,270 --> 00:08:50,520 ‫We can do anything we want. 166 00:08:50,520 --> 00:08:55,520 ‫It doesn't have to be related with class musicians at all. 167 00:08:55,770 --> 00:08:59,700 ‫So let me do an example so you will understand it better. 168 00:08:59,700 --> 00:09:01,830 ‫So if I initialize this 169 00:09:01,830 --> 00:09:04,380 ‫and this and this last one, 170 00:09:04,380 --> 00:09:07,110 ‫the error will go away, okay? 171 00:09:07,110 --> 00:09:11,220 ‫And I will just print something inside of my init function. 172 00:09:11,220 --> 00:09:15,570 ‫So I can say musician created for example. 173 00:09:15,570 --> 00:09:20,570 ‫So whenever I create an object, it will print this lock. 174 00:09:21,360 --> 00:09:24,210 ‫So let me create this, James. 175 00:09:24,210 --> 00:09:27,450 ‫As you can see, this is only creating the James 176 00:09:27,450 --> 00:09:31,710 ‫and this will only print out the James dot name 177 00:09:31,710 --> 00:09:36,710 ‫but before it prints, it says musician created. 178 00:09:37,050 --> 00:09:39,450 ‫So whenever I create another instance 179 00:09:39,450 --> 00:09:44,450 ‫of this musician class, it'll again print musician created. 180 00:09:45,360 --> 00:09:49,863 ‫So it isn't related with properties or attributes at all. 181 00:09:50,700 --> 00:09:52,920 ‫Of course we gonna make it related. 182 00:09:52,920 --> 00:09:56,100 ‫But let me do this example as well. 183 00:09:56,100 --> 00:09:59,640 ‫So I'm creating another instance from these musicians. 184 00:09:59,640 --> 00:10:02,520 ‫And if I run this you will see there will 185 00:10:02,520 --> 00:10:05,343 ‫be two musician created lock. 186 00:10:06,540 --> 00:10:10,380 ‫So in init you can write what you wanna do when 187 00:10:10,380 --> 00:10:12,720 ‫an object is created. 188 00:10:12,720 --> 00:10:16,080 ‫So this is Initializer, okay? 189 00:10:16,080 --> 00:10:19,260 ‫And again, you may have heard this 190 00:10:19,260 --> 00:10:24,090 ‫as a constructor in other programming languages. 191 00:10:24,090 --> 00:10:27,180 ‫What we actually wanna do in here is to 192 00:10:27,180 --> 00:10:31,290 ‫get some name agent instrument property values 193 00:10:31,290 --> 00:10:36,290 ‫and assign them as our properties, as our attributes. 194 00:10:36,840 --> 00:10:38,760 ‫We are gonna get those values 195 00:10:38,760 --> 00:10:42,390 ‫from the developer in Main dot Swift. 196 00:10:42,390 --> 00:10:46,503 ‫So let's stop here and do that within the next lecture.