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