1 00:00:00,270 --> 00:00:02,280 ‫-: Hi! Within this lecture, 2 00:00:02,280 --> 00:00:06,210 ‫we're going to write our first swift code. 3 00:00:06,210 --> 00:00:08,730 ‫So, this is a big thing for us, right? 4 00:00:08,730 --> 00:00:11,610 ‫So far we have connected our views 5 00:00:11,610 --> 00:00:13,920 ‫to buttonClicked function over here, 6 00:00:13,920 --> 00:00:16,320 ‫and, uh, we have our ImageView 7 00:00:16,320 --> 00:00:19,470 ‫right below our ViewController. 8 00:00:19,470 --> 00:00:21,900 ‫So when you see these curly braces, 9 00:00:21,900 --> 00:00:24,420 ‫it means a coding block, okay? 10 00:00:24,420 --> 00:00:27,390 ‫So we have a coding block in the class, 11 00:00:27,390 --> 00:00:31,770 ‫and we have a coding block inside of this buttonClicked. 12 00:00:31,770 --> 00:00:36,330 ‫So we write our codes inside of these blocks. 13 00:00:36,330 --> 00:00:39,120 ‫So right now this buttonClicked 14 00:00:39,120 --> 00:00:42,450 ‫created a coding block for us, 15 00:00:42,450 --> 00:00:45,270 ‫and we're going to write what will happen 16 00:00:45,270 --> 00:00:49,200 ‫when a button is clicked inside of this function. 17 00:00:49,200 --> 00:00:51,633 ‫And here you see something called viewDidLoad. 18 00:00:52,830 --> 00:00:55,890 ‫This is automatically generated for us, 19 00:00:55,890 --> 00:00:59,190 ‫as you can see, and this is what 20 00:00:59,190 --> 00:01:02,640 ‫will happen when I open the app first. 21 00:01:02,640 --> 00:01:07,640 ‫So this is the first time a ViewController is loaded. 22 00:01:08,190 --> 00:01:10,680 ‫So if I want something to happen 23 00:01:10,680 --> 00:01:15,630 ‫when user opens their app, I write it here. 24 00:01:15,630 --> 00:01:17,700 ‫But I don't want something to happen 25 00:01:17,700 --> 00:01:20,910 ‫when we first open the app, I want something to happen 26 00:01:20,910 --> 00:01:24,480 ‫when I first click the button, right? 27 00:01:24,480 --> 00:01:28,470 ‫So I'm going to write my code under this coding block 28 00:01:28,470 --> 00:01:33,450 ‫rather than viewDidLoad. We're going to go into the details 29 00:01:33,450 --> 00:01:36,150 ‫of viewDidLoad later on. For right now, 30 00:01:36,150 --> 00:01:38,070 ‫come over here to buttonClicked 31 00:01:38,070 --> 00:01:40,170 ‫and we're going to call the ImageView 32 00:01:40,170 --> 00:01:44,580 ‫that we have defined. As you can see, ImageView refers 33 00:01:44,580 --> 00:01:48,660 ‫to the ImageView that has been defined by us. 34 00:01:48,660 --> 00:01:53,430 ‫If you say, imageView dot, you can see the properties, 35 00:01:53,430 --> 00:01:55,560 ‫you can see the methods and attributes 36 00:01:55,560 --> 00:01:59,490 ‫of the ImageView. What do I mean by attributes? 37 00:01:59,490 --> 00:02:01,920 ‫For example, when we were doing 38 00:02:01,920 --> 00:02:04,560 ‫something in the main dot storyboard, 39 00:02:04,560 --> 00:02:09,240 ‫we can change, we changed actually the attributes 40 00:02:09,240 --> 00:02:11,280 ‫from the attributes pane, right? 41 00:02:11,280 --> 00:02:13,920 ‫Now, I can reach all of these attributes 42 00:02:13,920 --> 00:02:17,610 ‫and even more from here, from the code. 43 00:02:17,610 --> 00:02:22,610 ‫Now, if I say, imageView dot, I can see the attributes 44 00:02:23,070 --> 00:02:26,760 ‫like image in here. As you can see, 45 00:02:26,760 --> 00:02:30,840 ‫it gives us a description, so this is the image displayed 46 00:02:30,840 --> 00:02:35,550 ‫in the imageView. So, if I choose this property, 47 00:02:35,550 --> 00:02:40,233 ‫I can just change this, okay, inside of the code. 48 00:02:41,310 --> 00:02:46,310 ‫And, again, this is going to change the imageView.image 49 00:02:48,660 --> 00:02:52,200 ‫when I write this, so let me show you what else. 50 00:02:52,200 --> 00:02:55,870 ‫So, this will ask us to create an UIImage. 51 00:02:57,720 --> 00:03:02,720 ‫So, this is kind of, an object type, okay? 52 00:03:02,790 --> 00:03:07,260 ‫We're going to need a UIImage type object 53 00:03:07,260 --> 00:03:09,480 ‫in order to change this. So I'm going 54 00:03:09,480 --> 00:03:13,683 ‫to type UIImage, and open parentheses. 55 00:03:14,790 --> 00:03:17,970 ‫So you don't understand what a UIImage right now 56 00:03:17,970 --> 00:03:20,490 ‫because you don't know what a class is. 57 00:03:20,490 --> 00:03:23,040 ‫And you will understand this code 58 00:03:23,040 --> 00:03:26,280 ‫later on very clearly, but for right now, 59 00:03:26,280 --> 00:03:29,970 ‫let me write the code and you will see what I mean. 60 00:03:29,970 --> 00:03:31,950 ‫If you open parentheses, you will see 61 00:03:31,950 --> 00:03:34,290 ‫some different options here. I'm going 62 00:03:34,290 --> 00:03:38,280 ‫to go for names, because I'm going to create my image 63 00:03:38,280 --> 00:03:43,280 ‫from a name. I already have my metallica2 image over here. 64 00:03:44,700 --> 00:03:49,700 ‫If I say, metallica2 here, in the quotation marks, okay? 65 00:03:50,160 --> 00:03:53,160 ‫So, open a quotation and this is double quotation marks, 66 00:03:53,160 --> 00:03:57,210 ‫not a single quotation, so double quotation marks 67 00:03:57,210 --> 00:04:01,620 ‫if I say, metallica2, it will find the metallica2 image 68 00:04:01,620 --> 00:04:06,330 ‫in my project. It will convert it to be an image, 69 00:04:06,330 --> 00:04:10,110 ‫and it will display this image in my imageView. 70 00:04:10,110 --> 00:04:14,850 ‫So, that's what it stands for. ImageView is the object 71 00:04:14,850 --> 00:04:17,430 ‫that I have created, so let me run 72 00:04:17,430 --> 00:04:20,190 ‫in this simulator by the way, so, 73 00:04:20,190 --> 00:04:23,910 ‫while it compiles, I will explain the code one more time. 74 00:04:23,910 --> 00:04:26,640 ‫ImageView is the object that I have defined 75 00:04:26,640 --> 00:04:31,020 ‫over here. That image, same that change the image attribute 76 00:04:31,020 --> 00:04:34,530 ‫and if I click on change, as you can see, 77 00:04:34,530 --> 00:04:39,530 ‫the image actually changes to UIImage named metallica2. 78 00:04:42,150 --> 00:04:44,370 ‫So if I clicked on the change again, 79 00:04:44,370 --> 00:04:46,260 ‫it won't change one more time 80 00:04:46,260 --> 00:04:48,990 ‫because it's already metallica2 image. 81 00:04:48,990 --> 00:04:51,150 ‫And you may think at this point, 82 00:04:51,150 --> 00:04:55,230 ‫how do I make an app that, if I click on this change, 83 00:04:55,230 --> 00:04:58,827 ‫it will change the image to metallica1 84 00:04:58,827 --> 00:05:02,730 ‫and if I click on that it will change to metallica2? 85 00:05:02,730 --> 00:05:05,040 ‫And, we're going to do that later on. 86 00:05:05,040 --> 00:05:07,320 ‫Don't worry about that. We're going to see 87 00:05:07,320 --> 00:05:09,420 ‫how we can achieve this result, 88 00:05:09,420 --> 00:05:11,160 ‫but for right now, you don't know 89 00:05:11,160 --> 00:05:14,520 ‫anything about swift yet, so we have 90 00:05:14,520 --> 00:05:17,700 ‫to learn the basics first. This is just 91 00:05:17,700 --> 00:05:21,450 ‫to get you comfortable with Xcode and congratulations 92 00:05:21,450 --> 00:05:25,170 ‫on your first app by the way. You have just written an app 93 00:05:25,170 --> 00:05:28,350 ‫in which you can change the favorite band image 94 00:05:28,350 --> 00:05:31,380 ‫of yours. So let's stop here and we're going 95 00:05:31,380 --> 00:05:33,423 ‫to continue with the next one.