Introduction
In this article we will be covering a detailed discussion and implementation of Swift Typealias. The article includes declaration of Typealias, builtin variable, user defined variable and complex data type. Swift Typealias is used to give an existing data type in the application a new name. You may use the aliased name instead of the existing name throughout the application once you've created a typealias.
Typealias does not create a new data type; instead, it gives an existing data type a new name. It increases the code readability If you use them in a smart way they will really be useful. Let us see the codes in action below.
Declaration of TypeAlias
We can declare typealias as follows:
typealias name = existing type
For most types in Swift, you can use typealias.They can either be:
1. Built-in varieties (for.eg: String, Int)
2. Types that are defined by the user (for.e.g: class, struct, enum)
3. Types of complexity (for e.g: closures)
TypeAlias for Built-In variables
Typealias may be used with all built-in data types, including String, Int, and Float.
For Example:
typealias ProgrammingLanguage = String
The following declaration allows StudentName to be used in place of String everywhere.
So, say you want to make a string constant that represents anything like a student name.
You can accomplish the following:
let language:ProgrammingLanguage = "C++14"
You should declare a string constant without using typealias as:
let language:String = "C++14"
Both of the instances above produce a String constant. However, using typealias makes our code more understandable.
TypeAlias for user defined types
You may need to construct your own data type in a variety of situations. If you wish to build a Type that represents a student, you may use a class to do so.
class ProgrammingLanguage {
}
An array of programming language may now be expressed as follows:
var programmingLanguage:Array<ProgrammingLanguage> = []
Create your own type for Array<ProgrammingLanguage> and use typealias as follows to make the above declaration more readable:
typealias ProgrammingLanguages = Array<ProgrammingLanguage>
We can now make our code more understandable by adding:
var programmingLanguage:ProgrammingLanguages = []
TypeAlias for Complex DataTypes
Let's look at another case. Assume we have a method that accepts a closure as an argument.
Don't be concerned if you are unaware of the closures. Consider it a distinct sort of function. Swift closures is a detailed explanation of this topic.
func someMethod(oncomp:(Int)->(String)){
}
SomeMethod receives a closure as an input in the example above. The closure returns a String from an Int value.
As you can see, the reader is confused by the usage of (Int)->(String).You may give it a new name by using typealias:
typealias CompletionHandler = (Int)->(String)
You can now rewrite the method as follows:
func someMethod(oncomp:CompletionHandler){
}
With the usage of typealias, the identical code seems more explicit and programmer friendly.