Swift TypeAliasSwift 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. Purpose of using TypealiasThe main purpose of using typealias is make our code clearer and human readable. Create a typealiasTypealias is declared using the keyword typealias: Swift facilitates you to use typealias for most of the types:
Typealias for Built-in TypesTypealias can be used for all built-in data types i.e. String, Int, Float etc. For example:Here, we have declared EmployeeName as typealias of String. So, we can use it later instead of String type. For example:Without using Typealias, the declaration would be: By creating Typealias EmployeeName, we can write the same above declaration as: You can see that both examples create the same constant type string but the later one is more understandable for human. Typealias for user defined typesIn Swift, you can create your own data type. Suppose you have to create a data type Employee, so you can create it using a class as: Now, you can create a group of employees in an array as: Here, you can create your own type for array Now, the declaration would look like: It is easy to understand in your code. Next TopicSwift Enum |