1 00:00:01,340 --> 00:00:07,950 Solo come back again in this lesson we're going to do something quite simple and quite often useful. 2 00:00:07,990 --> 00:00:10,830 And that is how to start a timer. 3 00:00:11,150 --> 00:00:17,120 So I'm going to go to school with this thought a action when I start the new playground this time you 4 00:00:17,130 --> 00:00:18,930 haven't done that in a while. 5 00:00:19,130 --> 00:00:20,760 So when I say play along. 6 00:00:20,930 --> 00:00:24,070 This one is going to be time. 7 00:00:24,260 --> 00:00:28,240 They go on. 8 00:00:29,560 --> 00:00:29,890 They're 9 00:00:35,520 --> 00:00:35,900 out. 10 00:00:35,930 --> 00:00:38,950 So what they want to do is this it's a very simple thing. 11 00:00:38,960 --> 00:00:46,130 And then I say class I'm going to call it my class and then my class is going to have essentially two 12 00:00:46,130 --> 00:00:53,810 functions one that runs a timer and the other one that happens every time the timer runs itself. 13 00:00:53,960 --> 00:01:03,550 So a function through the trigger timer a function that the timer calls. 14 00:01:03,680 --> 00:01:07,240 So essentially one of them is like saying hey run the timer. 15 00:01:07,260 --> 00:01:12,590 The other one is saying so in every Sikander every two seconds or every 10 seconds what do you want 16 00:01:12,590 --> 00:01:13,660 to happen. 17 00:01:13,670 --> 00:01:14,860 So that's a simple. 18 00:01:14,900 --> 00:01:22,660 When I say function and my function I'm going to call it a run timer has no promulgators no return type 19 00:01:23,090 --> 00:01:34,200 in the body I have only one command timer DOD schedule time of it's time intervals and user info. 20 00:01:34,310 --> 00:01:36,290 So there's a lot of information here. 21 00:01:36,290 --> 00:01:39,840 Let me quickly explain what you have to do with them. 22 00:01:40,100 --> 00:01:43,540 So the time interval is how often you want something to happen. 23 00:01:43,550 --> 00:01:47,510 I'm going to say every socket and this a floating number. 24 00:01:47,510 --> 00:01:53,740 So you could for instance say every every millisecond every Thousands of a second. 25 00:01:53,890 --> 00:01:59,420 What fallen out one target is going to be self there's going to be a function right in here. 26 00:01:59,690 --> 00:02:04,110 The selector is going to be a method that we need to add. 27 00:02:04,130 --> 00:02:06,860 So that's the other function I'm going to say. 28 00:02:06,890 --> 00:02:16,600 The other one is called the let's call it preens fine grained time. 29 00:02:16,820 --> 00:02:21,830 So all we're going to do is going to print time now but you can simply imagine how you would expand 30 00:02:21,830 --> 00:02:29,320 on this to make a clock to make even make an hour long closed by using transformations. 31 00:02:29,450 --> 00:02:30,650 So on and so forth. 32 00:02:30,650 --> 00:02:37,580 But for now we're just going to say print on that print is going to say right for instance. 33 00:02:37,580 --> 00:02:40,680 Now my selector is going to be this method. 34 00:02:41,060 --> 00:02:48,420 Now here is a change that you have to essentially be aware of as of X Strief version 4. 35 00:02:48,560 --> 00:02:53,620 You need to expose anything that is using a select or two or Objective C. 36 00:02:53,630 --> 00:02:58,130 That means you have to have an Objective C in it. 37 00:02:58,270 --> 00:03:01,790 And I will show you in a second what happens if you don't have it. 38 00:03:01,850 --> 00:03:08,220 Let me say there is no use anything for let's say it is going to be repeating itself. 39 00:03:08,280 --> 00:03:11,070 Let me opt out and we'll see the error. 40 00:03:11,660 --> 00:03:21,260 So now it says argument of selector is not exposed to Objective-C and this is a necessity as of swift 41 00:03:21,260 --> 00:03:22,450 version 4. 42 00:03:22,700 --> 00:03:33,050 So if you want we can actually go to a terminal and say X code wrong Strief the dash version and it 43 00:03:33,220 --> 00:03:42,590 blue I am using version 4 point two that is essentially a command from the export command line tools. 44 00:03:42,620 --> 00:03:48,590 If you don't have the execute command line tools which offers a lot of flexibility when you're working 45 00:03:48,590 --> 00:03:52,040 with projects and we'll see it sometime in the future as well. 46 00:03:52,090 --> 00:03:58,400 But for now if you don't have anything in between a story you can of course do a quick search for it 47 00:03:58,490 --> 00:04:05,260 to come command you will end up meeting is X code select dash dash and saw and that's kind of style 48 00:04:05,820 --> 00:04:08,230 X code command line tools. 49 00:04:08,390 --> 00:04:13,400 So ex-coach space dash select and then install. 50 00:04:13,760 --> 00:04:16,100 So that's not what we're doing. 51 00:04:16,100 --> 00:04:21,590 We want to know the version of the swift that we using they're using 4.1 or anything of all four. 52 00:04:21,740 --> 00:04:29,320 You need to expose your functions in selectors to an Objective C by calling Objective C. 53 00:04:29,390 --> 00:04:33,020 I'm guessing sometime in the future are they going to they're going to change that. 54 00:04:33,040 --> 00:04:35,110 But for now this is how it works. 55 00:04:35,300 --> 00:04:38,300 So let's go ahead and use this time. 56 00:04:38,330 --> 00:04:44,930 So to use that I'm going to go ahead and say I have a variable called my object which is of course my 57 00:04:44,930 --> 00:04:50,920 class and then my object draws the wrong time. 58 00:04:51,200 --> 00:04:53,140 So it runs that time. 59 00:04:53,270 --> 00:04:56,060 Let's go ahead and see what that does for me. 60 00:04:57,240 --> 00:04:58,940 And it says hi. 61 00:04:59,050 --> 00:04:59,710 Hi. 62 00:04:59,800 --> 00:05:00,440 Hi. 63 00:05:00,490 --> 00:05:01,600 Hi. 64 00:05:01,600 --> 00:05:05,500 It constantly says hi once every once again. 65 00:05:05,500 --> 00:05:11,280 Now let's go ahead and make this a little bit more exciting than Mythili dad so I can get myself a little 66 00:05:11,350 --> 00:05:14,020 bit of the spaces in here. 67 00:05:14,150 --> 00:05:15,890 The Dow was though. 68 00:05:16,070 --> 00:05:16,970 All right. 69 00:05:17,140 --> 00:05:18,210 So here I'm going to do. 70 00:05:18,210 --> 00:05:19,810 I'm going to set you my class. 71 00:05:19,810 --> 00:05:28,000 I have something called our odd time and that's going to actually be a float. 72 00:05:28,060 --> 00:05:31,980 It actually has a value of zero at the very beginning. 73 00:05:32,470 --> 00:05:36,400 And then here is what's going to happen every time we go to print time. 74 00:05:36,410 --> 00:05:44,770 We're going to same time plus a call let's add a 10 office I can't. 75 00:05:44,770 --> 00:05:48,730 And then let's go there every tenth office Hikind. 76 00:05:48,900 --> 00:05:55,900 Now when I say preens time is on time is going to be time. 77 00:05:55,960 --> 00:06:03,220 Now if we run this you will notice that the float interpellation doesn't return us the exact numbers 78 00:06:03,220 --> 00:06:04,630 we were expecting. 79 00:06:04,630 --> 00:06:12,100 So we kind of meet the way of rounding it or showing only one similar space and to do that I'm actually 80 00:06:12,100 --> 00:06:15,720 going to change the print argument in here. 81 00:06:16,090 --> 00:06:25,020 I'm going to say the argument is I want is a strong format and the format I want is going to be percentage 82 00:06:25,050 --> 00:06:30,830 dot to one decimal space and the value is time itself. 83 00:06:30,970 --> 00:06:33,170 So essentially I'm saying format is fine. 84 00:06:33,170 --> 00:06:37,430 You at one decimal space and print it for me. 85 00:06:37,610 --> 00:06:39,180 Let's run this again. 86 00:06:39,650 --> 00:06:42,750 And now we get exactly what we expected it to. 87 00:06:42,760 --> 00:06:44,830 Three point forty four point one. 88 00:06:44,920 --> 00:06:51,640 So on and so forth if you wanted to I could go ahead and say call every hundredths of a second and give 89 00:06:51,640 --> 00:06:55,940 me tuus faces for instance is going to run a lot faster. 90 00:06:57,230 --> 00:07:02,450 In a lot of cases you want to go every one second or anything of the source. 91 00:07:02,630 --> 00:07:07,490 Of course you know how to connect all of these things into variables so you don't need to change it 92 00:07:07,580 --> 00:07:08,380 entirely. 93 00:07:08,380 --> 00:07:13,130 Places can change it only in one place and that's about that. 94 00:07:13,140 --> 00:07:15,950 Running a very simple timer. 95 00:07:16,070 --> 00:07:18,310 So I'll see in the next lesson.