Aug 07, 2022 iOS

Different Ways to add Typealias in Swift

Swift Typealias is used in the programme to provide a new name to an existing data type. You can use the aliased name instead of the existing name throughout the application once you establish a typealias.

This post will teach you about typealias and its applications in Swift.

A type alias lets you to give an existing data type a new name in your application. After declaring a type alias, the aliased name can be used in place of the existing type throughout the programme.

Type aliases do not result in the creation of new types. They merely give an existing type a new name.

The primary goal of typealias is to make our code more readable and clear in context for human comprehension.

Typealias does not create a new data type; rather, it gives an existing data type a new name.

Creating a Typealias

name of typealias = existing type

Swift makes it easy to utilise typealiases for most types:

  • Types that are built-in (For example, String, Int)
  • User Defined (For example, class, struct, and enum)
  • Types of Complexity (for e.g: closures)

Built-in Typealias

Typealias may be used for all built-in data types such as String, Int, Float, and so on.

As an example:

typealias Studenttype = String

The aforementioned declaration permits Studenttype to be used in instead of String everywhere. So, say you want to establish a string constant that represents anything like a student name. You can accomplish the following:

let Studentname:StudentName = "Danila"

You should declare a string constant without using typealias as:

let String studentname = "Danila"


Both instances above generate a String constant. However, by using typealias, we may make our code more understandable.

User-defined types typealias.


You can define your own data type in Swift. Assume you need to build a data type called worker, and you may do so by creating a class like:

 class worker { 

}

You may now build an array of workers like follows:

var workers :Array = [] 


To make the code more legible, you may use Typealias to build your own array type:

typealias Workers = Array 


The declaration would now look like this:

var workers:Workers = [] 


 Clause Typealias swift Generic


To further distinguish the two suitable types, a typealias generic with where clauses can be constructed using the Swift keyword typealias:

struct Axis<T> {
    var a: T
    var b: T
}

struct Radius<T> {
    var p: T
    var q: T
}

// Create a typealias diagram for Axis and Radius with
// Numeric values only (Int, Double, Float, etc)
typealias Diagram<T> = 
    (Array<Axis<T>>, Array<Radius<T>>) where T: Numeric

func Diameter<T>(_ diagram: Diagram<T>) {
    // Perform Diameter
}

Using Extension with typealias swift

typealias Sum = Double
extension Sum {
    func total() -> Sum {
        return self * 10
    }
}
let add: Sum = 100
print(add.total())
// The extension affects Doubles too:
let addDouble: Double = 12.2
print(addDouble.total())

Output

1000
122

Type Aliases swift Protocol


Type alias also advantages associated type in protocols. The entire breadth of this subject is beyond the scope of this article. Consider the following example:

protocol Name {
associatedtype load: StringProtocol
}
struct Student: Name {
typealias load = String
}

 Typealias swift Tuple


In Swift, a typealias tuple is defined by using the typealias keyword followed by a tuple definition:

typealias Axis = (Double, Double)
typealias Radius = (Axis, Axis)

Typealias swift Closure

Assume we have a basic procedure that accepts a closure as input:

func define(sum: (Double) -> (Double)) {}

To highlight that the (Double) -> (Double) function type is a math operation, you may now build a typealias for it:

typealias AddSum = (Double) -> (Double)


The method may now be changed to utilize this typealias in the type of its argument:

func define(sum: AddSum) {}

Conclusion


TypeAlias appears to be quite useful, although it does have certain downsides. It may take some time to comprehend things for someone who is new to your codebase. You must constantly draw a boundary between appropriate and excessive use. I use typealias all the time, but I try to keep it in check.

I’m wondering about how you utilise typealias in your daily job. Please share your ideas and experiences in the comments.

Thank you for your time!

Index