0 1 00:00:00,290 --> 00:00:00,660 All right. 1 2 00:00:00,690 --> 00:00:04,110 So now that we've seen what extensions can do, 2 3 00:00:04,110 --> 00:00:08,960 let's use this newfound knowledge to reorganize some of our code. 3 4 00:00:09,540 --> 00:00:14,550 So in our WeatherViewController, you can see that we've adopted two protocols here, 4 5 00:00:14,550 --> 00:00:18,390 the UITextFieldDelegate as well as the WeatherManagerDelegate. 5 6 00:00:18,390 --> 00:00:24,660 So let's try and split out some extensions so that we separate out our code which is dealing with the 6 7 00:00:24,660 --> 00:00:28,920 text field, and the code which is dealing with our WeatherManager, 7 8 00:00:28,920 --> 00:00:36,150 so right at the bottom of our WeatherViewController.swift file, outside of our class 8 9 00:00:36,150 --> 00:00:36,960 WeatherViewController. 9 10 00:00:36,960 --> 00:00:44,430 So below this curly brace that ends the class, I'm going to add our extension. And this one, of course, 10 11 00:00:44,430 --> 00:00:53,220 has to have the same name as our WeatherViewController to extend that class. And this extension is 11 12 00:00:53,220 --> 00:01:02,700 going to adopt the UITextFieldDelegate. And inside this block of code, I'm going to move in all of 12 13 00:01:02,700 --> 00:01:13,080 the parts of my code that's relating to my text field, including my IBMAction searchPressed. 13 14 00:01:13,080 --> 00:01:16,590 So I'm going to move all of that code down into here. 14 15 00:01:16,590 --> 00:01:24,180 And because we've got our UITextFieldDelegate adopted here, we can now delete it at the top. 15 16 00:01:24,180 --> 00:01:24,450 All right. 16 17 00:01:24,480 --> 00:01:27,300 So that's one down, one to go. 17 18 00:01:27,300 --> 00:01:32,640 Now, the next extension I'm going to build is going to be adopting the WeatherManagerDelegate. 18 19 00:01:32,760 --> 00:01:38,700 So let's create another extension for our WeatherViewController, and this one is going to adopt the 19 20 00:01:38,700 --> 00:01:39,930 WeatherManagerDelegate. 20 21 00:01:39,980 --> 00:01:47,610 And then inside here, we're going to move the two WeatherManagerDelegate methods, 21 22 00:01:47,640 --> 00:01:50,150 the didUpdateWeather and the didFailWithError. 22 23 00:01:50,280 --> 00:01:55,670 So we're going to cut that out and move it in here. 23 24 00:01:55,830 --> 00:02:02,950 So now we can, again, delete this one and we're now just left with a WeatherViewController 24 25 00:02:03,010 --> 00:02:10,560 that is subclassing the UIViewController and all the code that's currently in here is now a lot simpler. 25 26 00:02:10,560 --> 00:02:17,130 And when we click over here, you can see we've now got our two extensions with all of the method names 26 27 00:02:17,160 --> 00:02:20,100 listed out and the property names at the top here. 27 28 00:02:20,550 --> 00:02:27,860 And the "P" stands for the properties that we've got in our WeatherViewController. So that's pretty neat, 28 29 00:02:27,880 --> 00:02:30,580 but we can make it even better. 29 30 00:02:30,580 --> 00:02:37,240 Notice how at the moment if we have a large file, we can see that we've got separate extensions, but we 30 31 00:02:37,240 --> 00:02:44,490 can actually separate them out into different sections using something called a MARK. 31 32 00:02:44,500 --> 00:02:46,260 So this is a comment. 32 33 00:02:46,540 --> 00:02:54,640 And then the word "MARK" in all caps, and then we're going to add a colon and a single dash line. 33 34 00:02:54,640 --> 00:03:00,610 And as soon as I write the dash line, you can see that Xcode has now created this new section for me. 34 35 00:03:01,330 --> 00:03:09,490 And up here, you can see we've now divided our file into two separate sections before the MARK and after 35 36 00:03:09,490 --> 00:03:10,000 the MARK. 36 37 00:03:10,660 --> 00:03:20,290 Now, this MARK is going to denote the UITextFieldDelegate section. And so now when we check up here, 37 38 00:03:20,290 --> 00:03:26,530 you can see we've got a section called the UITextFieldDelegate and that includes this extension of 38 39 00:03:26,530 --> 00:03:35,260 WeatherViewController with all of these methods. Now, you've noticed that whenever we start typing, 39 40 00:03:35,260 --> 00:03:43,570 say, a struct or a class, you can see that on the left-hand side here, we've got a little symbol that denotes 40 41 00:03:43,870 --> 00:03:52,510 that this is a code snippet where if we hit Enter, it'll expand the code with placeholders and the default 41 42 00:03:52,510 --> 00:03:56,680 syntax for creating, say, a class or a struct. 42 43 00:03:56,680 --> 00:03:59,740 Now, we can also create our own code snippets. 43 44 00:04:00,340 --> 00:04:04,770 So, for example, you might often want to create these MARK comments, 44 45 00:04:04,870 --> 00:04:07,820 so why do we create code snippet? 45 46 00:04:07,930 --> 00:04:14,530 All we have to do to create a code snippet is to select the part of the code that we want to reuse, 46 47 00:04:14,530 --> 00:04:19,960 and then right click on it and go to Create Code Snippet. 47 48 00:04:19,960 --> 00:04:26,780 So, now we've got this new code snippet created and we can give it a name. So I'll call it a Mark Comment. 48 49 00:04:26,780 --> 00:04:33,200 And in the summary, it's just going to say, "Adds a mark 49 50 00:04:33,210 --> 00:04:34,660 comment." 50 51 00:04:35,010 --> 00:04:37,370 And this is the code as it is right now. 51 52 00:04:37,410 --> 00:04:44,610 But I also want to have a placeholder like the ones that you saw in the code snippets. And the 52 53 00:04:44,610 --> 00:04:50,380 placeholder is going to be where I'm going to remind myself to add the name of the section. 53 54 00:04:50,460 --> 00:04:58,100 So in order to create a placeholder, we open a angled brackets and then add a pound sign or a hashtag, 54 55 00:04:58,110 --> 00:04:58,970 you might call it, 55 56 00:04:59,220 --> 00:05:01,190 and then we put the name of our placeholder. 56 57 00:05:01,560 --> 00:05:08,130 So I'm going to call that Section Heading. And then we close it off with, again, a pound sign and a closing 57 58 00:05:08,130 --> 00:05:09,090 angle bracket. 58 59 00:05:09,180 --> 00:05:12,930 And we've now created our very own placeholder. 59 60 00:05:12,960 --> 00:05:17,310 So, now we can go ahead and finally add a completion. 60 61 00:05:17,340 --> 00:05:22,860 So this is the sort of text that you write in order to bring up this code snippet. 61 62 00:05:22,860 --> 00:05:27,380 So I'm going to call that mark, and then go ahead and click Done. 62 63 00:05:27,540 --> 00:05:33,810 Now that I've got my code snippet, I can either go to the code snippets, and then simply just drag it 63 64 00:05:33,810 --> 00:05:42,570 in like so. Alternatively, as you saw earlier on, we created that completion short name. So I can simply 64 65 00:05:42,570 --> 00:05:50,460 start writing mark and go ahead and hit enter when I see my mark comment, and it'll insert that MARK comment 65 66 00:05:50,550 --> 00:05:51,030 in there. 66 67 00:05:51,480 --> 00:05:59,200 So in my section heading for this section, I'm going to call WeatherManagerDelegate. Now, when I go and 67 68 00:05:59,200 --> 00:06:04,240 look in my file structure, I can see I've got three clear sections. 68 69 00:06:04,240 --> 00:06:06,160 The first one is just the class, 69 70 00:06:06,160 --> 00:06:12,130 and then we've got all of our extensions, and we've given each extension a section heading to separate 70 71 00:06:12,130 --> 00:06:15,850 them out. Now in the next lesson, 71 72 00:06:15,990 --> 00:06:23,130 we're going to be adding the functionality of getting hold of the current location based on the GPS data 72 73 00:06:23,130 --> 00:06:26,120 that the phone has access to. 73 74 00:06:26,190 --> 00:06:29,090 That's yet to come. And I'll see you on the next lesson.