All right. 1
So now that we've seen what extensions can do, 2
let's use this newfound knowledge to reorganize some of our code. 3
So in our WeatherViewController, you can see that we've adopted two protocols here, 4
the UITextFieldDelegate as well as the WeatherManagerDelegate. 5
So let's try and split out some extensions so that we separate out our code which is dealing with the 6
text field, and the code which is dealing with our WeatherManager, 7
so right at the bottom of our WeatherViewController.swift file, outside of our class 8
WeatherViewController. 9
So below this curly brace that ends the class, I'm going to add our extension. And this one, of course, 10
has to have the same name as our WeatherViewController to extend that class. And this extension is 11
going to adopt the UITextFieldDelegate. And inside this block of code, I'm going to move in all of 12
the parts of my code that's relating to my text field, including my IBMAction searchPressed. 13
So I'm going to move all of that code down into here. 14
And because we've got our UITextFieldDelegate adopted here, we can now delete it at the top. 15
All right. 16
So that's one down, one to go. 17
Now, the next extension I'm going to build is going to be adopting the WeatherManagerDelegate. 18
So let's create another extension for our WeatherViewController, and this one is going to adopt the 19
WeatherManagerDelegate. 20
And then inside here, we're going to move the two WeatherManagerDelegate methods, 21
the didUpdateWeather and the didFailWithError. 22
So we're going to cut that out and move it in here. 23
So now we can, again, delete this one and we're now just left with a WeatherViewController 24
that is subclassing the UIViewController and all the code that's currently in here is now a lot simpler. 25
And when we click over here, you can see we've now got our two extensions with all of the method names 26
listed out and the property names at the top here. 27
And the "P" stands for the properties that we've got in our WeatherViewController. So that's pretty neat, 28
but we can make it even better. 29
Notice how at the moment if we have a large file, we can see that we've got separate extensions, but we 30
can actually separate them out into different sections using something called a MARK. 31
So this is a comment. 32
And then the word "MARK" in all caps, and then we're going to add a colon and a single dash line. 33
And as soon as I write the dash line, you can see that Xcode has now created this new section for me. 34
And up here, you can see we've now divided our file into two separate sections before the MARK and after 35
the MARK. 36
Now, this MARK is going to denote the UITextFieldDelegate section. And so now when we check up here, 37
you can see we've got a section called the UITextFieldDelegate and that includes this extension of 38
WeatherViewController with all of these methods. Now, you've noticed that whenever we start typing, 39
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
that this is a code snippet where if we hit Enter, it'll expand the code with placeholders and the default 41
syntax for creating, say, a class or a struct. 42
Now, we can also create our own code snippets. 43
So, for example, you might often want to create these MARK comments, 44
so why do we create code snippet? 45
All we have to do to create a code snippet is to select the part of the code that we want to reuse, 46
and then right click on it and go to Create Code Snippet. 47
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
And in the summary, it's just going to say, "Adds a mark 49
comment." 50
And this is the code as it is right now. 51
But I also want to have a placeholder like the ones that you saw in the code snippets. And the 52
placeholder is going to be where I'm going to remind myself to add the name of the section. 53
So in order to create a placeholder, we open a angled brackets and then add a pound sign or a hashtag, 54
you might call it, 55
and then we put the name of our placeholder. 56
So I'm going to call that Section Heading. And then we close it off with, again, a pound sign and a closing 57
angle bracket. 58
And we've now created our very own placeholder. 59
So, now we can go ahead and finally add a completion. 60
So this is the sort of text that you write in order to bring up this code snippet. 61
So I'm going to call that mark, and then go ahead and click Done. 62
Now that I've got my code snippet, I can either go to the code snippets, and then simply just drag it 63
in like so. Alternatively, as you saw earlier on, we created that completion short name. So I can simply 64
start writing mark and go ahead and hit enter when I see my mark comment, and it'll insert that MARK comment 65
in there. 66
So in my section heading for this section, I'm going to call WeatherManagerDelegate. Now, when I go and 67
look in my file structure, I can see I've got three clear sections. 68
The first one is just the class, 69
and then we've got all of our extensions, and we've given each extension a section heading to separate 70
them out. Now in the next lesson, 71
we're going to be adding the functionality of getting hold of the current location based on the GPS data 72
that the phone has access to. 73
That's yet to come. And I'll see you on the next lesson.