Managing unwieldy or verbose custom types can be a hassle, and ineffective in the long run. In this video, get an overview of type aliasing in Swift, and how it can be used to make your Swift code that much better.
- [Presenter] Type aliasing is a great tool for combining complex or unwieldy custom values into clear types. We're going to look at how to create a type alias for a tuple and a closure. So, here in our test variables we're going to revisit the attack tuple we used earlier. So, let's start with the new key word, type alias and we're going to give it a name, attack tuple and we're going to say that it is always going to be a type string, int and bool.
Now, what the type alias is actually doing, here is saying that whenever we use our new attack tuple type, it actually represents a tuple of type string, int and bool. You can see how this is useful to have compact code. Now, we can use a tag tuple, like any other type, so, we can declare a new variable called sun strike of type attack tuple and just fill it out. Sun strike for the sting, 35 for the integer and it's not rechargeable.
Now, just so you know, you can of course add names to your tuple types here, even with the type alias. Now, let's declare a type alias for a closure. Type alias, and I'm going to call this array, closure. It's going to have a parameter and it's going to be an array of integers and it's going to return void again. All right, so, let's take these for a spin, now.
Let's create a new function called return attack. It's not going to have any parameters but it is going to return an attack tuple type. Now, here, we're just going to return hammer sweep, 42 and true. Let's just print this out, return attack. There we go, name, damage, can recharge.
So, already we're seeing that our code is much cleaner and much more compact, with type aliasing. Now, to use our array closure, let's create a new function and this is going to be similar to our fetch hunters. We're going to fetch player scores. Now, as an input parameter, we're going to name it closure and it's going to be a type array closure.
Got to declare a quick array, here of a couple scores and I'm going to call our closure and feed into it our score's variable. Now, I'm going to call fetch player scores and hit enter and I'm going to capture this in a local variable, called scores and just inter-rate through it. For score, in scores, I'm just going to print them out.
I'm going to say a... Actually, we'll just print out score, here and at the top of the closure, just add a print statement, for fun, top scores. Great, now in our debug log, we've got top scores and we've got all the scores that we used in our closure. So, now, we've got everything we need to write clean, readable, swift functions, closures and type aliases.
- Starting new playgrounds and projects
- Variables and constants
- Writing single and multiline comments
- Core string methods
- Working with numbers
- Working with collections
- Creating arrays
- Working with sets
- Application control flow
- Writing functions
- Basic Swift classes and structs