Swift Type Aliases
After deleting the
typealias
and replacing it with a class of the same name, we didn’t have to worry about changing function definitions and property types all throughout our codebase. We made the changes locally, in one file, and most all the rest of our code still compiled. Pretty cool! Here’s the relevant portion of the pull request that made that change.[…]
Any time you use the same tuple type more than once, consider making a
typealias
. In this case, the code became a lot shorter and easier to skim and understand.[…]
Objective-C developers, burdened with arcane syntax for blocks, use C’s
typedef
to isolate that syntax strangeness in one place. And even though Swift’s closure syntax is awesome, we can still benefit from Objective-C’s example – we can use type aliases for closure signatures.[…]
I recommend using a descriptive
typealias
that is private to your file, and then extending thattypealias
so you can keep things neat and tidy. […] We’re still extending the view controller, but specifically we’re extending thetypealias
so that the extension has a helpful name. This is another way thattypealias
can help add semantic meaning to your code.
Too bad Swift extensions can’t be named directly, like Objective-C categories.