(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
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
Latest end-to-end Learn by Coding Projects (Jupyter Notebooks) in Python and R:
All Notebooks in One Bundle: Data Science Recipes and Examples in Python & R.
End-to-End Python Machine Learning Recipes & Examples.
End-to-End R Machine Learning Recipes & Examples.
Applied Statistics with R for Beginners and Business Professionals
Data Science and Machine Learning Projects in Python: Tabular Data Analytics
Data Science and Machine Learning Projects in R: Tabular Data Analytics
Python Machine Learning & Data Science Recipes: Learn by Coding
R Machine Learning & Data Science Recipes: Learn by Coding
Comparing Different Machine Learning Algorithms in Python for Classification (FREE)
There are 2000+ End-to-End Python & R Notebooks are available to build Professional Portfolio as a Data Scientist and/or Machine Learning Specialist. All Notebooks are only $29.95. We would like to request you to have a look at the website for FREE the end-to-end notebooks, and then decide whether you would like to purchase or not.