So, now that we've covered the registration Log In and Log Out parts of our app, it's time to take a moment 1
and just review the code that we've written and see if there's ways that we can improve it. 2
And one of the most obvious things is just the sheer amount of strings that we're starting to build 3
up in our app. 4
And remember that strings are kind of dangerous because when we type a string, we basically get rid 5
of any sort of help that Xcode gives us in terms of code checking. 6
So whereas when we write a method such as performSegue, if I made a typo in that method name, then I'm 7
going to be called to attention on that because I'm going to get an error which tells me, oh, it doesn't know 8
about this performfSegue. And if I look at that closely, as long as I'm sort of vaguely with it, 9
then I'll know, okay, I made a typo here. 10
And once I fix it, my errors are gone and my syntax highlighting comes back on. 11
But strings are different because when we create a string, we're essentially saying, "Yes, computer, I know 12
what's going on. I know that it has to be spelled this way," and we get no help from Xcode. 13
So it's really easy to mess up and accidentally make a typo or, say, you instead of having an uppercase "R," 14
you have a lower case "r," and this string identifier for our segue no longer matches the one that 15
we created up here. 16
And when that happens, then what our app is going to do is it's going to crash. 17
It's not going to be able to find a segue with that particular misspelled identifier that we had here. 18
So let's register another user: a@b.dot com, and 1, 2, 3, 4, 5, 6 as the password, 19
click register and our app now crashes. 20
And the reason, 21
well, it says that terminating app because there is no segue with identifier registering to chat. 22
So how could we make our lives a little bit easier, and more, importantly, how can we make our future lives 23
or our colleague's lives easier? 24
Well, we can reduce the number of times that we type strings in our app. 25
So, for example, once we set up a string, we want to make sure that we always access it using code, rather 26
than just typing it out like so. 27
And to do this, we're going to create something called a constants file. 28
So in our Flash Chat iOS13 folder, we're going to create a new file and this is going to be a simple 29
Swift File, and it's going to be called Constants.swift. 30
So let's go ahead and click Creates. And we don't actually need any imports, 31
all we need is just to create a brand-new structure called Constants. And inside this structure, 32
we're going to create some properties. 33
So the first one that I might want to create is called the registerSegue and I'm going to set this 34
to equal the string for the name of this segue. 35
So I'm just going to copy and paste it from here into our Constants file, and then I'm going to do the 36
same for my loginSegue which is this one down here. And this one's called LoginToChat spelled like this. 37
So let's paste that in here as a string as well. 38
And now there's one extra thing I can add to these lines of code to make my life a little bit easier. 39
I'm gonna add a keyword called "static" in front of the word "let." And what this does is it turns these 40
properties from what we would call an instance property, so a property that's tied to an instance or 41
an object created from the Constants struct. 42
But now once it has the "static" keyword in front of it, it means that this has now become a type property. 43
So it's a property that's associated with the Constant data type. 44
So what this means is that inside my RegisterViewController, instead of using this string, I can simply 45
write Constants.registerSegue. And you'll notice that here I'm not actually creating a new object 46
from Constants in which case I would be adding my parentheses here, 47
right? 48
So instead, I'm actually accessing the type which is Constants, and then I'm getting hold of that 49
registerSegue's 50
value. So let's take a look at what's actually happening behind the scene. 51
So in my structure, I'm going to create a what's called an instanceProperty and I'm going to set this 52
to equal just "ABC," and then I'm gonna create a static let, and this is called a typeProperty, 53
and this is going to be set to equal "123." 54
So, now when I want to use the value of instanceProperty, say, I wanted to print it, 55
then what I actually have to do, in this case, in order to get "ABC" printed, I actually have to create a 56
new object from my structure, 57
so I call it myStructure= MyStructure, and then add the parentheses at the end to notify that 58
I'm initializing a new structure from this blueprint. 59
And now my structure is a new instance from this struct. 60
So this particular instance has access to the instanceProperty "ABC." 61
So I can now print myStructure.instanceProperty. 62
Now, on the other hand, if I wanted to use my typeProperty, then I don't actually have to create a new 63
instance of myStructure at all. 64
I can simply just tap into the type which is myStructure, and then write .typeProperty. And I can 65
already go ahead and print this into the console. 66
So let's go ahead and run this part first and you should see "ABC," and then let's run this part and you'll 67
see "123." 68
So the typeProperty is associated with the type and the instanceProperties are associated with instances 69
that are created from the type. 70
So small differentiation but big effects. 71
I have a little bit of background reading for you as a reading assignment. 72
If you head over to this link, which I'll add to the course resources page, and you go to this button: 73
ON THIS PAGE, and select Type Properties, I want you to have a read a bit more about the background of 74
how Type Properties work, what they're like, and a little bit more detail on it. 75
And at the same time, if you take a look at Type Methods, then you'll notice that it's very similar to 76
what we saw with typeProperties. 77
So if you create a method, let's call it instanceMethods inside our structure, versus if you create these 78
static func which is now a typeMethod inside our structure, then you can activate or call those two 79
methods in the same way that we've done with our properties. 80
So that instanceMethod, the one, which just has the func keyword, requires an instance, 81
so this myStructure that I created on line 16, and then I can use the dot notation and call that 82
instanceMethod. 83
But with my typeMethod, all I need is to access the myStructure type, and then use the dot notation, 84
and I can access and call the typeMethod already without having to initialize an object from that structure. 85
And you can do the same thing for classes as well. 86
It's not something unique to structures. 87
Now by convention, you'll normally see a lot of people type instead of "Constants," 88
they'll have the name "K" for Constants because it's cooler to spell it with a "K." 89
Not really. 90
But it does mean that it's much shorter. 91
So whenever you need to access each of these segues, instead of typing "Constants," you'll just write 92
K.registerSegue or K.loginSegue like here, 93
and you'll know it comes from our Constants file because of that word "K." So, now I want you to head over 94
to the README file and take a look at the section called Constants. And I want you to copy this section 95
in between these three backticks and the closing backticks and paste it to replace what you've currently 96
got in your Constants file, and just check to make sure that the registerSegue and the loginSegue 97
strings actually do match the ones that you've got in the Main.storyboard. So the other thing I'm 98
gonna add is the name of our app as well. So I'm going to copy this string from my Chat View Controller 99
and also add it to my Constants right at the top. 100
So it's gonna be "static let" and it's going to be called appName, and then I'm going to put it in here. 101
So, now when we go through our Welcome View Controller, instead of having FlashChat, we'll have 102
K.appName. 103
And similarly, in our ChatViewController, the title is going to be K.appName. And now not only are 104
we making our code a lot safer by keeping everything all the same and all Constant, 105
we're also reducing the amount of code that we're having to write. And we can get Xcode to autosuggest, 106
and hit tab or enter to insert it instead. 107
So, now all that's left is to run your app and test out all the things that we've changed just now, 108
say, going from the Log In screen to the chat screen, going from the register screen to the chat screen, 109
and also that the app name and the animations are actually displaying correctly, just to make sure that you've 110
actually got your segues named exactly the same as the one that we've just pasted into the Constants 111
file. 112
And once you've done that and once you're happy with it, then head over to the next lesson where we're 113
going to get started building out a table view and populating this page with some messages. 114
So for all of that and more, I'll see you there.