This practice will be retired on May 31, 2020.
Solution: Thinking About Types2:30 with Pasan Premaratne
So your task here was to understand why we have these errors. 0:00 What's wrong with our code? 0:04 So let's work through each one, one by one. 0:05 So in the first case, 0:08 we start off by declaring a variable because we use the keyword var here. 0:09 And we assign it a boolean value. 0:15 In the next line we try to assign a string to that same variable. 0:17 Now the issue here is because Swift is type safe, once you have 0:22 assigned a type to a constant or variable it is forever that type alone. 0:27 Now because this is a variable, we can change this and 0:32 we can say, isReady = false. 0:36 Because remember variables can change and false is the same type. 0:40 So this is valid code, but the reason that 0:44 the next line is failing is because here we are trying to assign a string to it. 0:46 So if we get rid of that we'll get rid of that error. 0:51 And the next line of code on line 6 for me, the issue here, or let's start first. 0:54 We're declaring a constant with the key word let, named value, 0:59 and we're assigning a number 10 here. 1:03 In the next line, we're trying to multiply this value by 3.0 and 1:05 then we're assigning that value to a constant. 1:10 Now, this might seems confusing, because this should be a valid operation. 1:13 But again, the point here is type safety. 1:17 Ten is an integer on its own, so if you hold down the option key to get this 1:20 question mark and then click on the constant, you'll see that the type is int, 1:25 but 3.0 is a double value. 1:30 And again, you cannot perform an operation with two different types. 1:33 So here we're trying to multiply 10 which is an integer by 3.0, 1:38 which is a double value. 1:43 And while it seems like this should work, 1:44 there are a lot of underlying reasons why this won't. 1:47 So again, the problem here is we're trying to combine types and 1:49 we should avoid doing that. 1:53 So let's get rid of that line. 1:53 Now let me undo that. 1:55 If I were to simply get rid of the .0 this would work, 1:57 because 3 is an integer on its own. 1:59 Now in this last segment here we have a constant named name and 2:03 we're assigning a string, and we have an error, 2:08 because we are trying to assign another string to a constant. 2:10 So even though this is the same type, because name is a constant, 2:14 you cannot change the underlying value. 2:18 And the way we get rid of this error, 2:20 one way is of course to delete the next line of code, but 2:23 if we change this to a variable, everything should work out correctly. 2:26
You need to sign up for Treehouse in order to download course files.Sign up