Your First iOS & SwiftUI App: An App from Scratch

Feb 13 2023 · Swift 5.7, iOS 16, Xcode 14

Part 3: Coding in Swift

28. Type Inference

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 27. Variables & Constants Next episode: 29. Conclusion

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

Right now in Game.swift, we've reduced our points method to just a few lines of code. But good news, there's a way to reduce the code by a few more characters thanks to an amazingly handy feature of Swift called type inference. Let's take a look at this line of code right here where we're calculating the difference. So we are creating a constant called difference and we're setting it to the absolute value of target minus sliderValue. Well, it turns out that Swift is pretty smart. Swift knows that target is an integer. We said it is up here when we declare the property. It also knows sliderValue is an integer because we've said it will be up here where we define the parameter and it knows that this absolute value function takes an integer and returns an integer. So Swift knows all of the values involved here are integers and because of that, it can figure out for you that if you're setting difference equal to the resulting value, difference is also going to be an integer. And because of that, you don't actually need to specify in this case that difference is an integer. You can delete that and the code will still work. And when I hit Command + B, it builds. We don't have any issues and I can verify that Swift knows this is an int by holding down the Option key and clicking on difference. And right here it says difference is an int. This has been figured out automatically for us by the Swift compiler because we're setting it to an expression that evaluates to an int every time. We can do this a lot of places in our code to make it a lot shorter and concise and easier to read. And that's actually a best practice in Swift development to not specify the type if you don't have to. So I'm going to go ahead and delete it for awardedPoints also because again, it can calculate that 100, which is an integer minus difference, which is an integer, is also an integer. I can also Option + click on that to verify it. Up here at the top of the file, target is clearly going to be an int because we're calling this random method on int to get one so you can delete the type from there. There's another common case where Swift can infer a type and that's when we use something called a literal. Score and round are both set to integer literals. Anytime you type a number without a decimal and there's nothing else saying it's some other type, Swift will assume you mean it should be an int. And since we're setting these initial values with zero and one, we don't need to specify the int there either. I can Option + click to verify all of those. Let's go to ContentView and we can clean up some of these as well. We're setting this to false, which is one of the two possible values of a Boolean. So I don't need to say that it's a Boolean. This 50.0 is a double literal. When you include a decimal in a number, Swift assumes you mean a double. So I'll go ahead and delete that. And what about game? Well, we're setting this to an instance of game so we don't need to say it's a game again. So delete that too. And there's another case that we've done this down here. We're saying rounded value is an int but we don't need to do that. We're already saying it is when we create the int here. All right, so our code is already looking a lot more concise thanks to Swift type inference but I wanna show you how we can reduce our point method even more. Back in Game.swift, we can truly get this down to just one line. So we have a couple of constants here in a row, but we can simplify this. So instead of making a constant for awardedPoints and returning awardedPoints, we can just directly say return 100 minus the difference. But we don't even really need the difference constant. We can just say return 100 minus the result of this absolute value function. Just copy and paste the code down there. And there's actually one more way that we can simplify this. If you have a method that just has a single line of code in it, you don't actually need to put the return beforehand. We've said the method returns a value up here with a return token. So Swift will assume since a value has to be returned from this method and you've got code here evaluating to the right type, it should just return the result of this line of code so I can remove the return as well. And you can see this has now become extremely short. I've come a long way from that long if-else statement to this nice little line of code and it's looking really good. So let me run my test again with Command + U. All looking good, everything is still working. And if you'd like, you're welcome to run this in the simulator to try it out manually yourself with command + R. I'll do my best to match the target. Tap Hit Me, close the alert. Perfect.