in , ,

Swift Typealias

Swift Typealias
Swift Typealias

Swift Typealias: In this tutorial, you will learn about typealias and its use cases in Swift.

A typealias in Swift is literally an alias for an existing type. Simple, isn’t it? They can be useful in making your code somewhat more readable. By using them in a brilliant manner they can be really useful in your codebase.

A type alias allows you to give a new name to an existing data type into your program. After a type alias is declared, the aliased name can be used rather than the existing sort all through the program.

Type alias don’t create new sorts. They essentially give a new name to a current type.

The main purpose typealias is to make our code more readable, and clearer in setting for human arrangement.


How to create a typealias?

It is declared using the watchword typealias as:

typealias name = existing type

In Swift, you can use typealias for most types. They can be either:

• Built-in types (for.eg: String, Int)

• User-defined types (for.e.g: class, struct, enum)

• Complex types (for e.g: closures)


Typealias for built-in types

You can use typealias for all built in data Types as String, Int, Float and so forth

For instance:

typealias StudentName = String

The above declaration allows StudentName to be used wherever as opposed to String. In this way, on the off chance that you need to create a constant of type string but addresses more like student name. You can do as:

let name:StudentName = "Salman"

Without using typealias, you ought to declare consistent of type string as:

let name:String = "Salman"

Above the two examples creates a constant of type String. But, declaring with typealias, our code turns out to be more readable.


Typealias for user defined types

There are numerous situations when you need to create your own data type. Assume you need to create a Type that addresses Student, you can create it using a class as:

class Student {

}

Presently a group of students can be addressed as an array as:

var students:Array<Student> = []

The above declaration can be made more readable by creating your own sort for Array<student> using typealias as:

typealias Students = Array<Student>

Presently we can make our code more readable as:

var students:Students = []

Typealias for complex types

Lets examine one more example. Assume we have a technique that takes a closure as an input parameter.

Try not to stress in the event that you don’t think about closures. Simply consider it as a special type of function. We have clarified it detail in the tutorial: Swift closures.

func someMethod(oncomp:(Int)->(String)){

}

The above example takes a closure as an input to someMethod. The closure takes an Int value and returns string.

You can see the use of (Int)- >(String) makes less sense to the reader. You can use typealias to provide a new name for it:

typealias CompletionHandler = (Int)->(String)

Presently you can change the strategy as:

func someMethod(oncomp:CompletionHandler){

}

We can see a similar code looks all the more clear and programmer friendly with the us of typealias.


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

Swift Closures

Swift Closures

csharp hello world

C# Hello World – Your First C# Program