Type aliases in Swift — Real tips

Photo by John Schnobrich on Unsplash

Original Post

Type aliases easy to use, but it is not clear how and when to use this. It is a default situation when you started to learn protocols for example. Protocols are easy to use and understand, but difficult to implement in a project without experience and real examples.

Default use. You can replace some existing Type on typealias. For example Int on Age. And Age is more convenient to use then Int in this case.

Tuples replace. You can replace some tuples on convenient typealias. And it is easy to use these types in the whole project.

Closure replace. It is very useful to replace some huge closure to convenient typealias.

Few required types replace. Protocol-Oriented Programming and various architectures like VIPER make some problems in Type defining (Check my VIPER post here). Problems start when you need that some variable will have two or more types. It is disgusting for code readability and it is cool to have typealias in this case

Combine with Generic use. The closure is widely used in cross modules communication (as like delegate pattern). And you always need to send different data with different types. But it is kind messy without typealias.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store