Typealias In Swift

Yasir
2 min readJul 1, 2020

--

In this article you will learn |Typealias| 🚀

Introduction

Swift Typealias is used to provide a new name for an existing data type in the program. Once you create a typealias, you can use the aliased name instead of the exsisting name throughout the program.

Typealias doesn’t create a new data type, it simply provides a new name to the existing data type. It does not make any effect to your code , its just make your code more readable and easy .

Why we use it ?

The reason is why we use in a line is to just make your code clear and readable . By using them in a smart way they can be really useful in your codebase.

Typealias in Swift

You can use swift typealias with most of the types :

  • Built-in types
  • User define types
  • Complex types

Typealias for Built in types

Instead of using String, let us create a fake name for String.

typealias Name = String

func insertName(name: Name) {}
insertName(name: "Mohd Yasir")

Typealias for User Define types

class Employee {}

typealias MyEmployees = [Employee]
func listEmployees(enterEmployees: MyEmployees) {}

--

--

Yasir
Yasir

Written by Yasir

Exploring Swift, SwiftUI, and Apple Universe.

No responses yet