0
1
00:00:00,540 --> 00:00:06,420
So in the last lesson, we said that we were going to shorten these five lines of code using the Swift
1

2
00:00:06,420 --> 00:00:08,400
ternary operator.
2

3
00:00:08,400 --> 00:00:13,890
So the way of the ternary operator works is that we have a condition, just as we have conditions
3

4
00:00:13,980 --> 00:00:15,080
in if statements,
4

5
00:00:15,090 --> 00:00:20,400
we have this condition and we check to see if it's true or if it's false. If it's true,
5

6
00:00:20,430 --> 00:00:24,630
then we set this value to a particular value if it's true.
6

7
00:00:24,960 --> 00:00:27,530
And if it's not true, then we set it to something else.
7

8
00:00:27,540 --> 00:00:32,720
So let's try and convert our five lines of code to this format.
8

9
00:00:32,850 --> 00:00:39,930
So the value in this case is the cell.accessoryType and we're going to set it to equal the condition
9

10
00:00:40,020 --> 00:00:44,390
which is the same as what's between our If statement and our opening bracket.
10

11
00:00:45,660 --> 00:00:56,520
So whether if item.done is equal to true. And if it is true, then the cell.accessoryType is going
11

12
00:00:56,520 --> 00:01:06,950
to be set to .checkmark. But otherwise, if it's not true, then the cell.accessoryType is going to
12

13
00:01:06,950 --> 00:01:09,910
be set to equal .none.
13

14
00:01:10,280 --> 00:01:16,590
And that's a one line expression that does exactly the same thing as all of these lines.
14

15
00:01:16,850 --> 00:01:21,120
And we can make it even shorter by getting rid of this check.
15

16
00:01:21,590 --> 00:01:29,720
So this line now reads set the cell's accessoryType depending on whether the item.done is true.
16

17
00:01:30,110 --> 00:01:33,140
If it is true, then set it to .checkmark.
17

18
00:01:33,170 --> 00:01:36,140
And if it's not true, then set it to .none.
18

19
00:01:36,140 --> 00:01:41,990
So depending on how much you've used this ternary operator or how much you've seen it used, you'll
19

20
00:01:42,000 --> 00:01:45,210
begin to get comfortable with this expression.
20

21
00:01:45,440 --> 00:01:49,610
And admittedly, when you first see it, it looks like magic, right?
21

22
00:01:49,610 --> 00:01:56,390
It just doesn't look like a real code because it's so short and it's, you know, some would argue not as
22

23
00:01:56,390 --> 00:02:03,590
explicit as this. Now, because we've come such a long way, and a big shout out to everybody who's been
23

24
00:02:03,590 --> 00:02:04,570
following along
24

25
00:02:04,580 --> 00:02:11,960
up till now, you've amassed a lot of knowledge. And previously in my code, I've tried to make the code
25

26
00:02:12,020 --> 00:02:18,190
as explicit as humanly possible even though sometimes it's very, very cumbersome.
26

27
00:02:18,200 --> 00:02:24,410
Now, as we're getting into the intermediate and advanced stages, I want to make sure that you learn and
27

28
00:02:24,410 --> 00:02:30,680
you can understand some of these ways that programmers will express their code to cut down on the number
28

29
00:02:30,680 --> 00:02:34,910
of lines and make that code look more succinct and more elegant.
29

30
00:02:34,940 --> 00:02:39,950
So when you come across something like this in the future, you'll no longer be confused as to what it
30

31
00:02:39,950 --> 00:02:40,580
means.
31

32
00:02:41,560 --> 00:02:44,500
So, now I can safely delete all of that.
32

33
00:02:44,830 --> 00:02:52,060
And if we run our app, let's see if despite deleting something like 10 lines of code, whether if our app
33

34
00:02:52,060 --> 00:02:53,590
still works exactly the same,
34

35
00:02:53,650 --> 00:02:54,790
let's just confirm.
35

36
00:02:54,790 --> 00:02:58,750
So I can safely confirm that our app works exactly the same as before.
36

37
00:02:58,840 --> 00:03:04,270
Even though we're making everything super short and super succinct, it's still behaving exactly as we
37

38
00:03:04,270 --> 00:03:04,880
wanted to.
