1
00:00:07,000 --> 00:00:10,140
Everybody what's going on says Caleb with deps Lopes dot com.

2
00:00:10,150 --> 00:00:11,190
And in this video.

3
00:00:11,260 --> 00:00:12,010
Get excited.

4
00:00:12,010 --> 00:00:14,740
We're going to create our model layer.

5
00:00:14,740 --> 00:00:15,450
All right.

6
00:00:15,580 --> 00:00:20,890
This app that we're going to build is basically going to just display some information about a particular

7
00:00:20,920 --> 00:00:22,640
apple product that we want to buy.

8
00:00:22,780 --> 00:00:28,450
So in order to actually set this up we're going to go ahead and create a class that will act as our

9
00:00:28,450 --> 00:00:36,370
model layer to create instances of our Apple product so pull open your project we're going to right

10
00:00:36,400 --> 00:00:39,700
click on the model folder in X code.

11
00:00:39,940 --> 00:00:42,540
We're going to click new file like so.

12
00:00:42,940 --> 00:00:50,210
And we don't need to do anything other than a basic swift file for a model class case so go ahead and

13
00:00:50,210 --> 00:01:00,650
we're going to name this Apple product and go ahead and click Create your class we'll pull up here.

14
00:01:00,820 --> 00:01:04,960
And the cool thing is we shouldn't actually need to use anything other than Foundation.

15
00:01:04,990 --> 00:01:11,020
The most core fundamental swithe types and that is true or I guess we could also use custom types but

16
00:01:11,020 --> 00:01:12,130
that doesn't require foundation.

17
00:01:12,130 --> 00:01:16,180
So anyway let's move on and create our class.

18
00:01:16,180 --> 00:01:20,230
Now you should know about classes you should have a good fundamental understanding of the swift programming

19
00:01:20,230 --> 00:01:22,630
language before diving into this course.

20
00:01:22,810 --> 00:01:27,920
So let's create a class called Apple product.

21
00:01:28,420 --> 00:01:34,720
OK so remember this is our model layer so we need to think what is the blueprint what are we going to

22
00:01:34,720 --> 00:01:43,180
help them build we can think of our model class as an instruction set to build an Apple product so we

23
00:01:43,180 --> 00:01:51,430
can create both properties and functions K properties being variables or constants are things about

24
00:01:51,460 --> 00:01:53,680
our class that make it that way.

25
00:01:53,680 --> 00:02:01,120
So an Apple product for instance has a name iPhone 10 an Apple product has a color space gray or silver

26
00:02:01,600 --> 00:02:08,110
an Apple product has a price 399 499 999 $15 billion.

27
00:02:08,380 --> 00:02:14,550
It has a price but there are other things that make Apple products Apple products right.

28
00:02:14,560 --> 00:02:18,720
They can do stuff they can make calls they can send messages they can connect to the Internet.

29
00:02:18,730 --> 00:02:21,370
They can do all kinds of amazing things.

30
00:02:21,370 --> 00:02:23,990
So that's where functions would come in in a class.

31
00:02:24,100 --> 00:02:30,610
But for now we're just going to go ahead and create some properties that make an Apple product an Apple

32
00:02:30,610 --> 00:02:31,540
product.

33
00:02:31,540 --> 00:02:39,760
And so we're going to go ahead and begin by creating the variable var name and a name is a text data.

34
00:02:39,760 --> 00:02:45,520
So we should use the type String var name is going to be of type string.

35
00:02:45,740 --> 00:02:45,990
OK.

36
00:02:46,000 --> 00:02:50,620
And it's going to tell me class Apple product has no initializers.

37
00:02:50,620 --> 00:02:55,810
That's because we have not yet created an initialiser function but we will next.

38
00:02:55,810 --> 00:02:57,730
So OK our Apple product has a name.

39
00:02:57,730 --> 00:02:58,750
Next we need.

40
00:02:58,750 --> 00:03:02,740
How about a color var color.

41
00:03:02,860 --> 00:03:05,880
And you know what we could use all kinds of different things.

42
00:03:05,890 --> 00:03:12,460
But let's just go ahead and use a string and pass in the word for whatever color our Apple device will

43
00:03:12,460 --> 00:03:14,460
be next.

44
00:03:14,470 --> 00:03:15,640
How about a price.

45
00:03:15,640 --> 00:03:17,770
Var price.

46
00:03:17,800 --> 00:03:24,970
And we're going to go ahead and use the number type double which includes a floating point decimal value

47
00:03:25,210 --> 00:03:30,510
because you know the price is going to come in with a certain number of dollars and cents.

48
00:03:30,550 --> 00:03:31,430
So there's that.

49
00:03:31,510 --> 00:03:34,270
We have a name we have color we have price.

50
00:03:34,420 --> 00:03:37,110
And let's just leave it at that for now.

51
00:03:37,480 --> 00:03:40,030
So now it says we have no initializers.

52
00:03:40,150 --> 00:03:47,050
We need to create a function that is going to essentially run through and set all the values for this

53
00:03:47,050 --> 00:03:52,170
particular model layer and create an instance of it in memory that we can use.

54
00:03:52,180 --> 00:03:58,120
So to do that go ahead and just call init and pass in three parameters.

55
00:03:58,120 --> 00:04:02,320
We have one name of type String.

56
00:04:02,320 --> 00:04:05,830
We have a second one color also of type string.

57
00:04:05,830 --> 00:04:07,420
And apparently I can't type.

58
00:04:07,630 --> 00:04:08,290
And we have.

59
00:04:08,290 --> 00:04:10,870
Price of type double.

60
00:04:10,990 --> 00:04:17,110
Now in order to set these properties all we need to do is to type self-caused name referring to this

61
00:04:17,110 --> 00:04:18,070
property here.

62
00:04:18,130 --> 00:04:23,890
We're going to set it to be equal to the name property passed in from the function and to do that we

63
00:04:23,890 --> 00:04:25,890
just need to call name.

64
00:04:25,990 --> 00:04:32,080
Then below that we're going to call self-caused color and set it to be color the 10:01 from the function

65
00:04:32,650 --> 00:04:35,380
beneath that type self price.

66
00:04:35,530 --> 00:04:39,360
And we're going to pass in price just like that.

67
00:04:39,400 --> 00:04:45,760
So we have now successfully created the class but there's a problem a very big problem and I'm going

68
00:04:45,760 --> 00:04:48,970
to show you what that is in a future video.

69
00:04:48,970 --> 00:04:50,730
This is not a very safe class.

70
00:04:50,800 --> 00:04:55,000
We're going to show you a better way to make it safer in a future video.

71
00:04:55,000 --> 00:04:56,560
But for now this will work.

72
00:04:56,560 --> 00:04:59,120
We have a class called Apple product.

73
00:04:59,140 --> 00:05:00,920
It has three properties.

74
00:05:01,060 --> 00:05:04,780
Name color and price and it also can be initialized.

75
00:05:04,780 --> 00:05:08,460
We can create an instance of it and then use it in our application.

76
00:05:08,500 --> 00:05:16,270
So cool so to show you how this works I'm actually going to create a constant called Apple product and

77
00:05:16,270 --> 00:05:22,020
I'm going to set it to be equal to Apple product and watch this if I go ahead and put an open parentheses.

78
00:05:22,150 --> 00:05:28,690
It's going to ask me to autocomplete and if I do you'll see I can pass in three properties.

79
00:05:28,690 --> 00:05:30,870
This is our initialiser function being called.

80
00:05:31,000 --> 00:05:37,170
So for my name I might say iPhone 10 for the color I might say space gray.

81
00:05:37,510 --> 00:05:43,840
And for the price I might say nine hundred ninety nine point ninety nine for the retail price of the

82
00:05:43,870 --> 00:05:45,480
iPhone 10.

83
00:05:45,850 --> 00:05:50,520
Now the cool thing what I can do here is I can access these properties.

84
00:05:50,590 --> 00:05:56,980
So now that I've created an instance of Apple product here and this constant I could print out Apple

85
00:05:56,980 --> 00:06:03,130
product Dot and I could choose any of those properties Apple product name and I could print that to

86
00:06:03,130 --> 00:06:04,190
the console.

87
00:06:04,270 --> 00:06:06,500
I could print out Apple product color.

88
00:06:06,760 --> 00:06:11,980
I could print out a Apple product up price if I were to cast it as a string but it's not because it's

89
00:06:11,980 --> 00:06:12,680
double.

90
00:06:12,700 --> 00:06:17,020
But anyway this is it guys we can now create an instance of our class.

91
00:06:17,050 --> 00:06:19,200
But like I said there are some issues here.

92
00:06:19,240 --> 00:06:22,600
These variables have an issue and we're going to fix it in a future video.

93
00:06:22,750 --> 00:06:24,010
But guys amazing work.

94
00:06:24,010 --> 00:06:30,730
We now have our model layer Apple product and we can use that to create an instance of our data model.

95
00:06:30,730 --> 00:06:32,720
Very very cool stuff guys.

96
00:06:32,830 --> 00:06:38,650
In the next video we're going to dive into the world of view and we're going to create a custom view

97
00:06:38,890 --> 00:06:43,840
that we can actually display on the screen and I'll show you how it works I'll show you how we can interface

98
00:06:44,110 --> 00:06:48,550
with the View Controller and help the View Controller to present that view.

99
00:06:48,550 --> 00:06:51,260
So let's head over there now and I'll see you there.
