Swift programming for Beginners – Swift Typealias

(Swift for Beginners)

Swift Typealias

In this article, you will learn about typealias and its use cases in Swift.

A type alias allows you to provide a new name for an existing data type into your program. After a type alias is declared, the aliased name can be used instead of the existing type throughout the program.

Type alias do not create new types. They simply provide a new name to an existing type.

The main purpose of typealias is to make our code more readable, and clearer in context for human understanding.


How to create a typealias?

It is declared using the keyword 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 etc.

For example:

typealias StudentName = String

The above declaration allows StudentName to be used everywhere instead of String. So, if you want to create a constant of type string but represents more like student name. You can do as:

let name:StudentName = "Jack"

Without using typealias, you should declare constant of type string as:

let name:String = "Jack"

Above both examples creates a constant of type String. But declaring with typealias, our code becomes more readable.


Typealias for user defined types

There are many cases when you need to create your own data type. Suppose you want to create a Type that represents Student, you can create it using a class as:

class Student{

}

Now a group of students can be represented as an array as:

var students:Array<Student> = []

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

typealias Students = Array<Student>

Now we can make our code more readable as:

var students:Students = []

Typealias for complex types

Lets analyze one more example. Suppose we have a method that takes a closure as an input parameter.

Don’t worry if you do not know about closures. Just think of it as a special type of function. We have explained it detail in the article: 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)

Now you can rewrite the method as:

func someMethod(oncomp:CompletionHandler){

}

We can see the same code looks more clear and programmer friendly with the use of typealias.

 

Swift programming for Beginners – Swift Typealias

 

Personal Career & Learning Guide for Data Analyst, Data Engineer and Data Scientist

Applied Machine Learning & Data Science Projects and Coding Recipes for Beginners

A list of FREE programming examples together with eTutorials & eBooks @ SETScholars

95% Discount on “Projects & Recipes, tutorials, ebooks”

Projects and Coding Recipes, eTutorials and eBooks: The best All-in-One resources for Data Analyst, Data Scientist, Machine Learning Engineer and Software Developer

Topics included: Classification, Clustering, Regression, Forecasting, Algorithms, Data Structures, Data Analytics & Data Science, Deep Learning, Machine Learning, Programming Languages and Software Tools & Packages.
(Discount is valid for limited time only)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.

Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners

Please do not waste your valuable time by watching videos, rather use end-to-end (Python and R) recipes from Professional Data Scientists to practice coding, and land the most demandable jobs in the fields of Predictive analytics & AI (Machine Learning and Data Science).

The objective is to guide the developers & analysts to “Learn how to Code” for Applied AI using end-to-end coding solutions, and unlock the world of opportunities!