1 00:00:00,540 --> 00:00:06,680 This lesson is an exercise to show a random e-mail depending on a touch of a button. 2 00:00:06,780 --> 00:00:09,260 I was of course using swithe. 3 00:00:09,270 --> 00:00:14,730 So for this lesson I've actually provided you with some photos that you know this the same set of photos 4 00:00:14,730 --> 00:00:17,560 that I've been using through and through this entire application. 5 00:00:17,660 --> 00:00:23,460 Of course you could Art Of course your own photos if you wanted to or if not you can just stick to my 6 00:00:23,460 --> 00:00:24,060 photos. 7 00:00:24,240 --> 00:00:32,540 Let's go ahead to X code and what is this project I'm going to start a new project called random photo. 8 00:00:36,110 --> 00:00:44,390 So in the random photo here is what you want to see if we want to see that you have a button up here. 9 00:00:45,310 --> 00:00:49,280 And that then says Ah. 10 00:00:49,810 --> 00:00:50,570 Let me just get. 11 00:00:50,670 --> 00:00:54,320 Better background color so I can see it myself. 12 00:00:54,370 --> 00:00:57,450 Show random image. 13 00:00:57,580 --> 00:01:02,900 And then there is an image of you and this image of you is somebody in here. 14 00:01:03,110 --> 00:01:03,570 Right. 15 00:01:03,580 --> 00:01:09,390 And then every time that we stop on this one it should show us of one of those pictures. 16 00:01:09,380 --> 00:01:13,480 One of these photos that I'm going to add to my project. 17 00:01:13,480 --> 00:01:15,360 So you have to do everything in swift. 18 00:01:15,400 --> 00:01:16,820 Now here's the catch. 19 00:01:16,840 --> 00:01:19,690 I don't want to see a repetitive picture. 20 00:01:19,750 --> 00:01:25,540 It means if you tap the first time and you saw the picture of the birds the next time you top birds 21 00:01:25,540 --> 00:01:27,330 should be able to be shown. 22 00:01:27,430 --> 00:01:33,250 So the next time we should show one of the other four objects of our photos and then so on and so on 23 00:01:33,250 --> 00:01:35,610 and so on until you finish the array. 24 00:01:35,740 --> 00:01:36,990 I already gave it away. 25 00:01:36,990 --> 00:01:38,070 It's an array. 26 00:01:38,090 --> 00:01:41,230 And then you have to restart everything. 27 00:01:41,230 --> 00:01:46,570 So at any given point to similar photos shouldn't be shown back to back. 28 00:01:46,750 --> 00:01:48,340 So good luck with that. 29 00:01:48,340 --> 00:01:50,270 Try telling your audience with. 30 00:01:50,370 --> 00:01:56,050 I'm going to do it as well if you want to compose the video at this point and then come back to it and 31 00:01:56,050 --> 00:01:57,710 see my solution as well. 32 00:02:00,370 --> 00:02:01,340 So let's get started. 33 00:02:01,350 --> 00:02:03,090 Here is how I'm going to do it. 34 00:02:03,100 --> 00:02:06,070 First thing is I'm going to go to my view. 35 00:02:06,490 --> 00:02:13,810 I know I want to content's more to be aspect Phil and I when I clip it to the boundaries I want to go 36 00:02:13,810 --> 00:02:20,610 to my assistant editor get myself a little bit more face up here don't worry I will clean this up later. 37 00:02:20,620 --> 00:02:29,830 I have one function called show around on action and it's called the type action the outlet. 38 00:02:29,830 --> 00:02:34,960 And I also have this image of you so I'm going to call it my homage. 39 00:02:35,140 --> 00:02:36,000 That's about that. 40 00:02:36,000 --> 00:02:37,210 That's all I have. 41 00:02:37,420 --> 00:02:43,240 And then I'm going to go to my editor and the editor I'm going to you know make things nice and tidy 42 00:02:43,240 --> 00:02:45,090 the way I usually like to make them. 43 00:02:45,490 --> 00:02:49,920 And because this function is happening much later on I'm going up with at the bottom. 44 00:02:50,110 --> 00:02:52,180 So here are the things that you have to do. 45 00:02:52,180 --> 00:02:56,490 We have to first of all put our images inside an array. 46 00:02:56,650 --> 00:03:07,540 So first of all I'm going to go up here say let images array which is of type array and of the type 47 00:03:07,540 --> 00:03:10,350 of what you are images. 48 00:03:10,420 --> 00:03:18,370 So you I imagine it's going to be a brand new array or if you want just make it like that. 49 00:03:18,490 --> 00:03:25,220 Either way you initialize your array or if you want you can actually place your objects in there. 50 00:03:25,240 --> 00:03:28,490 So I'm going to actually go ahead and put my objects in there. 51 00:03:28,660 --> 00:03:36,190 I'm going to say one of my objects is called What is it called Barretts GBG. 52 00:03:36,460 --> 00:03:39,100 But this is not a new image. 53 00:03:39,100 --> 00:03:40,900 This is actually a string. 54 00:03:40,900 --> 00:03:43,230 What I'm looking for the UI image. 55 00:03:43,240 --> 00:03:44,440 So I'm what I have to say. 56 00:03:44,470 --> 00:03:48,980 Well you I image the name. 57 00:03:49,300 --> 00:03:57,230 And here is the name that I want very similar to you I image you made named of of and were using Objective-C 58 00:03:57,610 --> 00:03:59,130 now is giving me an arrow. 59 00:03:59,170 --> 00:04:03,640 And this is the first time we're seeing this kind of error and that error is because of the optional 60 00:04:03,670 --> 00:04:05,160 binding he's saying. 61 00:04:05,200 --> 00:04:09,920 Are you sure this object exists and I'm going to say absolutely. 62 00:04:10,050 --> 00:04:12,480 And if you want we can actually go to. 63 00:04:12,490 --> 00:04:13,910 Let me show you the error. 64 00:04:13,990 --> 00:04:19,780 The actual content of it always actually giving us two options one has to choose a default value or 65 00:04:19,780 --> 00:04:25,750 the other by saying force on rapid so that you know that thing you know exists and if it is an optional 66 00:04:25,750 --> 00:04:29,870 value that contains nil it's gonna abort the execution is going to fail. 67 00:04:30,020 --> 00:04:31,420 But I say fix it like that. 68 00:04:31,420 --> 00:04:38,010 So odd that they are so boring that one line down and then I need to repeat this process several times 69 00:04:38,020 --> 00:04:38,350 right. 70 00:04:38,380 --> 00:04:40,720 So one two three four. 71 00:04:40,720 --> 00:04:42,360 How many are there five. 72 00:04:42,400 --> 00:04:43,980 I think that's about that. 73 00:04:44,110 --> 00:04:45,940 So one of them is called birds. 74 00:04:45,940 --> 00:04:48,860 The other one is called Chad the JP G. 75 00:04:48,880 --> 00:04:54,040 The other one is called the dog or EMG that GBG. 76 00:04:54,070 --> 00:04:58,050 The third one is called 12:56 is called Happy. 77 00:04:58,130 --> 00:04:59,510 JP G. 78 00:04:59,590 --> 00:05:05,640 And the last one a plant is called The Road image dot pitch perfect. 79 00:05:05,650 --> 00:05:13,720 So I've got all of my objects all of my photos inside an array of type II image which is exactly what 80 00:05:13,760 --> 00:05:16,720 we would need for a situation like this. 81 00:05:16,720 --> 00:05:22,890 So the next thing is this every time we stop in here we should get one and a random one. 82 00:05:23,050 --> 00:05:29,680 So let's first get at least one around and I'm going to say rather or less because we're not going to 83 00:05:29,680 --> 00:05:38,690 change that around index is going to be Orrick for random and arke Fernando is a unsigned integer 32. 84 00:05:38,710 --> 00:05:45,920 So I have to actually get an integer integer like for random and then I'm going to get that modulus 85 00:05:46,000 --> 00:05:49,270 two images array dot count. 86 00:05:49,300 --> 00:05:56,650 So essentially whatever number we have in here and we get that modulus this which is 5 we get any number 87 00:05:56,770 --> 00:06:02,840 between 0 2 4 0 1 2 3 and 4. 88 00:06:02,890 --> 00:06:03,660 Perfect. 89 00:06:03,690 --> 00:06:05,940 Once we got that here's what we're going to do. 90 00:06:05,980 --> 00:06:14,630 We're going to say my image of you that image becomes EMG array Rand index. 91 00:06:14,650 --> 00:06:21,760 So every time we press this random action bigger than your random location we show a new random image 92 00:06:21,760 --> 00:06:25,400 from the images array inside the image of our image. 93 00:06:25,660 --> 00:06:38,450 Let's see the once it builds launches hopefully here one two three four five six seven sometimes it 94 00:06:38,450 --> 00:06:39,760 is repeating itself. 95 00:06:39,770 --> 00:06:43,590 That's something we haven't you know are taking care of now. 96 00:06:43,610 --> 00:06:47,150 So what can we do today to remedy that situation. 97 00:06:47,150 --> 00:06:58,120 Here is my suggestion if in here in our randomisation if we got one if this random number was wrong 98 00:06:58,220 --> 00:07:06,050 vine and we used up of one and used up 1 let's remove it. 99 00:07:06,050 --> 00:07:13,610 So every time that we use an image you remove that from the array so it not even there to be reconsidered. 100 00:07:13,760 --> 00:07:15,390 So that's not really very simple. 101 00:07:15,470 --> 00:07:21,290 I'm going to say you merge array dots remover at which one do you want to remove. 102 00:07:21,290 --> 00:07:25,320 I want to remove the random index once I use that. 103 00:07:25,460 --> 00:07:29,630 This is now going to work but somehow it is going to actually. 104 00:07:29,950 --> 00:07:32,270 What record is going to crash or application. 105 00:07:32,270 --> 00:07:33,790 I will tell you why. 106 00:07:33,860 --> 00:07:39,710 We also have another issue which is actually a good thing that happened is telling me you cannot change 107 00:07:40,100 --> 00:07:47,150 a member of this immutable array simply because it's a let's say you become Baff. 108 00:07:47,300 --> 00:07:51,320 So you become a valuable so you can be changed later on. 109 00:07:51,320 --> 00:07:54,530 So based on that let's go ahead and rerun this. 110 00:07:54,580 --> 00:08:00,900 And like I said this is going to work but somehow it's going to crash now soon you will see why. 111 00:08:01,190 --> 00:08:09,580 First time is purrfect chat dog of birds Araud and nothing so far has been similar. 112 00:08:09,830 --> 00:08:17,540 And now boom is going to crash simply because the last time that we did this the last time that we saw 113 00:08:17,540 --> 00:08:25,250 whatever image we removed the very last image from our images array is not there anymore to be wrapped 114 00:08:25,250 --> 00:08:27,540 around again to be repeated again. 115 00:08:27,710 --> 00:08:32,770 So the solution to that is we don't touch our actual images array. 116 00:08:32,770 --> 00:08:37,740 We make another 3 majors are eight that we copy from here whenever we need. 117 00:08:38,000 --> 00:08:39,290 So let's see how we do that. 118 00:08:39,290 --> 00:08:44,630 I'm going to go up here say this image array is the one of the cheap. 119 00:08:44,690 --> 00:08:47,390 Don't touch it and since to be not going to touch it. 120 00:08:47,450 --> 00:08:48,230 I'm going to make it. 121 00:08:48,230 --> 00:08:55,770 Let's again then I'm going to make another e-mail just a recall value valuable current images. 122 00:08:55,790 --> 00:09:04,610 Ari these are the ones we are going to change and they're going to be an array of you image you image 123 00:09:05,150 --> 00:09:08,000 and they're going to be empty at the very very beginning. 124 00:09:08,210 --> 00:09:12,760 So here is how we are going to deal with this entire situation. 125 00:09:12,950 --> 00:09:23,660 Every time we go to shore random action I'm going to check if the current images are a dot count is 126 00:09:23,810 --> 00:09:24,740 zero. 127 00:09:24,980 --> 00:09:34,910 If we have used up every one in it if they are zero then current images array will become images array. 128 00:09:36,300 --> 00:09:41,820 So if you have used up every one read copy everything back into it. 129 00:09:41,820 --> 00:09:46,920 This obviously is going to happen the very very first time Batoche because the very very first time 130 00:09:47,220 --> 00:09:49,780 current image array that count is zero. 131 00:09:49,860 --> 00:09:50,880 There is nobody in it. 132 00:09:50,880 --> 00:09:57,130 So then the first time the top is going to say 0 0 or so let's go copy everyone now they copy everyone. 133 00:09:57,150 --> 00:09:59,100 Let's go and find a random index. 134 00:09:59,100 --> 00:10:02,720 Now who should we find the random index from. 135 00:10:02,760 --> 00:10:04,440 Not the images anymore. 136 00:10:04,460 --> 00:10:07,300 You have to find that from the current images. 137 00:10:07,680 --> 00:10:11,420 And then Image view should be read from our current images. 138 00:10:11,560 --> 00:10:18,600 And we finally have to remove from current images in this situation our image array essentially works 139 00:10:18,660 --> 00:10:20,060 as a backup. 140 00:10:20,100 --> 00:10:20,900 It's there. 141 00:10:20,900 --> 00:10:28,470 So whenever we need the copy from it it doesn't really do anything on its own is just there as a backup. 142 00:10:28,470 --> 00:10:35,550 Let me try to reformat this so maybe hopefully I can show you everything in in in one screen. 143 00:10:35,580 --> 00:10:37,030 I don't know if I'm going to succeed. 144 00:10:37,050 --> 00:10:41,640 Just bear with me for a second and that's good. 145 00:10:41,730 --> 00:10:42,870 I'm going to remove that. 146 00:10:42,870 --> 00:10:44,190 We know what that is. 147 00:10:44,460 --> 00:10:45,810 And then that's the other array. 148 00:10:45,810 --> 00:10:50,690 I'm going to do this even though I don't like it really like that. 149 00:10:50,700 --> 00:10:54,070 So they all are in front of their line. 150 00:10:54,330 --> 00:10:57,430 And yes everything is now within one screen. 151 00:10:57,450 --> 00:11:00,120 So we have our images array as a backup. 152 00:11:00,120 --> 00:11:04,660 We have our current images array the one we use the one we use is immutable. 153 00:11:04,680 --> 00:11:07,550 The one we don't want to use is immutable. 154 00:11:07,740 --> 00:11:11,070 We have an image view and of course we have our real function. 155 00:11:11,100 --> 00:11:14,570 We don't really need all of you not because we are not doing anything in. 156 00:11:14,580 --> 00:11:16,880 So you can actually drop it all together. 157 00:11:17,070 --> 00:11:23,430 And then in here in the short run I'm going to say if column image is that it doesn't have any one copy 158 00:11:23,430 --> 00:11:30,350 the images are into it and then every time finder and on one plus Don't into image view. 159 00:11:30,390 --> 00:11:35,810 And of course remove it from the current images Ali that see this app in action. 160 00:11:36,420 --> 00:11:43,290 So once it runs and launches on my simulator I should be able to see a brand new image every time my 161 00:11:43,280 --> 00:11:44,430 top. 162 00:11:44,550 --> 00:11:50,700 I should mention in the interest of total accuracy that it is possible to see two images that are identical 163 00:11:50,700 --> 00:11:51,510 back to bite. 164 00:11:51,660 --> 00:11:56,860 That is Von's one array and that ends another copy gets generate that. 165 00:11:56,970 --> 00:12:02,410 It's a rare situation but you could simply add an if statement and fix that. 166 00:12:02,640 --> 00:12:04,290 So that's about that. 167 00:12:04,530 --> 00:12:10,950 I would encourage you to try two other approaches such as remembering the previous index as opposed 168 00:12:10,950 --> 00:12:13,290 to copying everything in one array. 169 00:12:13,290 --> 00:12:18,400 What if you use only one array and remembered which one you have seen before. 170 00:12:18,510 --> 00:12:21,550 So you would you know you would avoid repeating it. 171 00:12:21,600 --> 00:12:26,010 There are many many ways this could be done if you had a better solution if you have an idea please 172 00:12:26,010 --> 00:12:27,420 share it with the rest of us. 173 00:12:27,630 --> 00:12:29,000 I see you in the next lesson.