1 00:00:00,620 --> 00:00:04,660 When we started this course we said that Apple made three considerations when 2 00:00:04,660 --> 00:00:09,070 building this brand new language of which one was safety. 3 00:00:09,070 --> 00:00:11,570 Swift is a type safe language. 4 00:00:11,570 --> 00:00:12,240 What does this mean? 5 00:00:13,690 --> 00:00:17,700 A type safe language encourages you to be clear about the types 6 00:00:17,700 --> 00:00:22,920 of values your code can work with If a variable expects a string, 7 00:00:22,920 --> 00:00:25,080 you cannot pass it it an int by mistake. 8 00:00:26,450 --> 00:00:28,680 We saw this earlier but let's check it out again. 9 00:00:29,790 --> 00:00:34,272 Let's add another comment marker to our Playground and 10 00:00:34,272 --> 00:00:36,855 we'll title this Type Safety. 11 00:00:40,379 --> 00:00:42,520 Let's declare a variable. 12 00:00:42,520 --> 00:00:49,600 We'll call it some string and will assign an empty string to it. 13 00:00:51,020 --> 00:00:55,120 Now what if we wanted to assign a value of type double to it. 14 00:00:55,120 --> 00:01:04,020 So we'll see some string Equal 12.0 of course when we try this we get an error. 15 00:01:04,020 --> 00:01:07,320 Cannot assign a double to type String. 16 00:01:08,440 --> 00:01:11,000 Okay let's comment this out. 17 00:01:11,000 --> 00:01:14,030 If you're coming from a different language like Python or 18 00:01:14,030 --> 00:01:15,930 Ruby this might seem odd to you. 19 00:01:16,940 --> 00:01:20,720 Those languages are called dynamically typed languages 20 00:01:20,720 --> 00:01:23,780 while Swift is a statically typed language. 21 00:01:23,780 --> 00:01:26,420 In Swift once you declare a variable or 22 00:01:26,420 --> 00:01:30,570 a constant to be a particular type it is always that type. 23 00:01:31,660 --> 00:01:37,210 Now over here we're declaring that some string is a container that always contains 24 00:01:37,210 --> 00:01:42,660 a string by assigning a string to it and the reason for this is that the compiler 25 00:01:42,660 --> 00:01:48,230 can then perform checks for you and make sure you're not making any mistakes. 26 00:01:48,230 --> 00:01:53,600 This helps us of void errors early on and reduce the number of bugs in our code. 27 00:01:53,600 --> 00:01:56,870 For example, let's say we have a form in our app and we asked for 28 00:01:56,870 --> 00:02:02,200 a user's zip code and were expecting an integer value to come out of that. 29 00:02:02,200 --> 00:02:07,230 Well because we can only accept an integer value, we can make sure that the user 30 00:02:07,230 --> 00:02:12,540 does not type anything else does not pass in floating point value for example. 31 00:02:12,540 --> 00:02:17,240 This work that the compiler does is known as type checking. 32 00:02:17,240 --> 00:02:20,890 But how does the compiler know which type we mean? 33 00:02:20,890 --> 00:02:22,800 Now we've been cheating a bit here and 34 00:02:22,800 --> 00:02:26,310 relying on the compiler to figure it out for us. 35 00:02:26,310 --> 00:02:30,930 Instead, we can explicitly declare the type that a constant holds, 36 00:02:30,930 --> 00:02:32,960 a constant or a variable. 37 00:02:32,960 --> 00:02:39,280 Okay so, down here lets declare a new constant, and we'll say let best player. 38 00:02:40,990 --> 00:02:45,500 Previously we would simply assign a string to this and the compiler would guess 39 00:02:45,500 --> 00:02:50,400 that the constants type was a string to explicitly declared the type. 40 00:02:50,400 --> 00:02:54,920 Immediately after the name we put a colon, a space and 41 00:02:54,920 --> 00:02:57,890 then the type that we want to set the value to. 42 00:02:57,890 --> 00:03:00,410 Which in this case is String. 43 00:03:00,410 --> 00:03:02,970 Then we can proceed as before and 44 00:03:02,970 --> 00:03:08,280 assign a value say Michael Jordan or if I want to start a war. 45 00:03:08,280 --> 00:03:12,220 I can say LeBron James when When you declare the type like this. 46 00:03:12,220 --> 00:03:16,220 The convention is to have no space between the variable or 47 00:03:16,220 --> 00:03:17,780 constant name and the colon. 48 00:03:17,780 --> 00:03:21,940 But is space between the colon and the type name. 49 00:03:21,940 --> 00:03:25,710 Now keep in mind that the name of the type is case sensitive. 50 00:03:25,710 --> 00:03:28,600 So if we were to say string with a lowercase S. 51 00:03:28,600 --> 00:03:30,280 We're going to get an error. 52 00:03:30,280 --> 00:03:33,540 Okay, so here we're explicitly declaring the type. 53 00:03:33,540 --> 00:03:37,140 We're saying that bestPlayer is going to contain a string. 54 00:03:37,140 --> 00:03:40,450 And bestPlayer can always only contain a string value. 55 00:03:41,960 --> 00:03:43,290 Let's add some more values. 56 00:03:43,290 --> 00:03:45,695 We'll stick to this sports analogy for this example. 57 00:03:45,695 --> 00:03:51,860 So we'll say, let averagePointsPerGame, 58 00:03:51,860 --> 00:03:59,470 now this we want to be a double value always and we're going to assign 30.1. 59 00:03:59,470 --> 00:04:03,380 We can say let yearOfRetirement, now 60 00:04:04,690 --> 00:04:09,830 this is going to be an integer value of type int. 61 00:04:09,830 --> 00:04:11,840 And he retired in 2003. 62 00:04:11,840 --> 00:04:15,570 And then we can say let halloffame. 63 00:04:15,570 --> 00:04:19,410 Now this is a Boolean value and also that this to true. 64 00:04:19,410 --> 00:04:23,810 Now in contrast to earlier when we used all these types. 65 00:04:23,810 --> 00:04:28,050 We're making it extremely explicit here as to what the types of the variables in 66 00:04:28,050 --> 00:04:33,800 constants are as in what values they contain now and, can always contain. 67 00:04:33,800 --> 00:04:37,600 Okay, so how did the compiler know earlier what the types were 68 00:04:37,600 --> 00:04:39,180 when we didn't mention them? 69 00:04:39,180 --> 00:04:44,430 This is because of a feature in Swift called type inference, type inference 70 00:04:44,430 --> 00:04:50,330 enables the compiler to deduce the type of a particular expression automatically by 71 00:04:50,330 --> 00:04:55,660 examining the values that we provide and assign to our constants or variables. 72 00:04:55,660 --> 00:04:56,610 For newcomers, 73 00:04:56,610 --> 00:05:01,130 this may not seem like a big deal that the compiler can deduce the type. 74 00:05:01,130 --> 00:05:04,720 But remember, Swift is the successor to Objective-C, and 75 00:05:04,720 --> 00:05:09,460 in Objective-C we were required to declare every single type. 76 00:05:09,460 --> 00:05:11,850 When the compiler infers the type for 77 00:05:11,850 --> 00:05:17,310 us, we say that the type annotation this is a type annotation 78 00:05:17,310 --> 00:05:22,150 we say that the type annotation is implicit or that we have an implicit type. 79 00:05:22,150 --> 00:05:26,510 Now again this may not seem that important or even relevant to you. 80 00:05:26,510 --> 00:05:30,650 Why does the compiler even need to know the type why can't we just provide 81 00:05:30,650 --> 00:05:34,310 any value and there are certain arguments to those and we'll get to those in 82 00:05:34,310 --> 00:05:38,790 the future, once you understand more about working with different kinds of data. 83 00:05:38,790 --> 00:05:44,100 So this begs the question, should you be declaring the type explicitly like 84 00:05:44,100 --> 00:05:49,620 we've done here or letting the compiler do the work for you like we've done here? 85 00:05:49,620 --> 00:05:52,250 Well, that is a matter of balance. 86 00:05:52,250 --> 00:05:57,900 Like comments, it's easier for others and future you to understand your code and 87 00:05:57,900 --> 00:06:03,230 what the types of variables and constants are when they're explicitly declared. 88 00:06:03,230 --> 00:06:07,720 Now my recommendation for you is, until you're really familiar with our types, 89 00:06:07,720 --> 00:06:11,320 until you know that this is a Bool, this is an Int, go ahead and 90 00:06:11,320 --> 00:06:12,930 explicitly write them out. 91 00:06:12,930 --> 00:06:16,860 Once you're used to them you can start letting the compiler do the work for you. 92 00:06:16,860 --> 00:06:19,150 So will revisit this discussion in the future. 93 00:06:20,450 --> 00:06:22,790 Type inference is a great thing but 94 00:06:22,790 --> 00:06:27,860 part of being a programmer is balancing readability with language features. 95 00:06:27,860 --> 00:06:31,000 As you improve and get used to writing more code, 96 00:06:31,000 --> 00:06:33,930 you can choose to ignore the type declaration, if you desire. 97 00:06:35,080 --> 00:06:36,770 Let's wrap this up here. 98 00:06:36,770 --> 00:06:38,500 We've learned some more knowledge and 99 00:06:38,500 --> 00:06:40,840 are well on our way to getting acquainted with Swift. 100 00:06:41,840 --> 00:06:45,230 I set you up with a quiz and a code challenge after this. 101 00:06:45,230 --> 00:06:47,670 Good luck and I'll see you in the next set of videos.