Table of contents
1.
Introduction
2.
Declaration of TypeAlias
2.1.
TypeAlias for Built-In variables
2.2.
TypeAlias for user defined types
2.3.
TypeAlias for Complex DataTypes
3.
Frequently Asked Questions
3.1.
What is type alias?
3.2.
What is the use of Typealias in Swift?
3.3.
What is tuple in Swift?
3.4.
What's the difference between lazy and Lateinit?
4.
Conclusion
Last Updated: Mar 27, 2024
Medium

TypeAlias in Swift

Author Aman Thakur
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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.

Frequently Asked Questions

What is type alias?

Type aliases provide alternative names for existing types. If the type name is too long you can introduce a different shorter name and use the new one instead. It's useful to shorten long generic types.

What is the use of Typealias in Swift?

A type alias lets you to give an existing data type a new name in your application. Following the declaration of a type alias, the aliased name can be used in place of the existing type throughout the programme. There are no new types created by type aliases. They merely give an existing type a new name.

What is tuple in Swift?

A tuple is a collection of various values in Swift. A tuple's values can also be of distinct data types. If we need to store information about a product's name and price, we may build a tuple containing a value for name (text) and another value for price (number) (float).

What's the difference between lazy and Lateinit?

Lateinit can only be used with a var property whereas Lazy will always be used with val property. A lateinit property can be reinitialised again and again as per the use whereas the lazy property can only be initialised once.

Conclusion

If you have reached till here than you have really enjoyed reading this article. This article covers  application of  typealias which may help you to enhance readability throughout your codebase. Check out what it can do for your project and make your code self-documenting. You can also checkout the article such as Environment Setup in Swift.

Do upvote our blog to help other ninjas grow, and head over to our practice platform Coding Ninjas Studio to practise top problems, attempt mock tests, read interview experiences, and much more.

Happy Learning!

Live masterclass