A variable is a named container that is used to store a value. Now each value has a type to which it belongs and is known as data type. A data type is a classification of data that tells the compiler how a programmer wants to use the data. Data types can be broadly classified into three categories, primitive, user-defined, and derived data types. To use an integer value Swift provides int data, to use a decimal value Swift provides Float or Double data type, and to use a collection of letters Swift provides String data type. Most of the languages provide a keyword using which we can refer data types with other names or alias also. In Swift, we have “typealias” keyword to refer to a data type with another name.
Type alias in Swift
Swift provides a functionality using which we can refer to a data type with another name or alias. Basically it provides a new name to the existing type and always remembers it does not create a new type. Swift provides a keyword “typealias” which is similar to “typedef” keyword in C++. We can define more than one alias or another name for a data type in Swift. The general syntax for typealias is given below,
Syntax:
typealias myDataType = dataType
Here, typealias is a keyword, dataType is one of primitive, user-defined, or complex data type, and myAlias is an alias to dataType i.e, now we can call datatype with the name myDataType. In Swift, broadly we can define an alias for the following data types:
1. Type alias for primitive data types
Swift provides a keyword using which we can refer to primitive data types with an alias or another name. For example, we can refer to Int data type using myInt, Float using myFloat, Double using myDouble. Internally, typealias doesn’t create new data types for existing data types. It just provides the alternate names for existing types.
Syntax:
typealias myDataType = dataType
Here, typealias is a keyword, dataType is one of the primitive data types, Int, Float, Double, etc., myDataType is an alias to dataType i.e, now we can call datatype with the name myDataType
Example:
In the below program we have used an alias or another name for primitive data types as,
- myInt for Int
- myFloat for Float
- myDouble for Double
- myCharacter for Character
- myString for String
Swift
typealias myInt = Int
typealias myFloat = Float
typealias myDouble = Double
typealias myCharacter = Character
typealias myString = String
var integerNumber : myInt
integerNumber = 5
print ( "integerNumber:" , integerNumber)
var floatNumber : myFloat
floatNumber = 5.12345
print ( "floatNumber:" , floatNumber)
var doubleNumber : myDouble
doubleNumber = 5.123456789
print ( "doubleNumber:" , doubleNumber)
var character : myCharacter
character = "A"
print ( "character:" , character)
var string : myString
string = "GeeksforGeeks"
print ( "string:" , string)
|
Output:
integerNumber: 5
floatNumber: 5.12345
doubleNumber: 5.123456789
character: A
string: GeeksforGeeks
2. Type alias for user-defined data types
Swift provides functionality using which we can refer user-defined data types with an alias or another name. For example, we can refer to Array<Int> data type using arrayInt, Array<Float>, using arrayFloat, Array<Double> using arrayDouble. Internally, typealias doesn’t create new data types for existing user-defined data types. It just provides the alternate names for existing user-defined types.
Syntax:
typealias myDataType = dataType
Here, dataType is one of the user-defined data-types, Array<Int>, Array<Float>, Array<Double>, etc., myDataType is an alias to dataType i.e, now we can call datatype with the name myDataType
Example:
In the below program we are creating a structure of “Employee“ type. It contains two variables, one of string type and the other is Int type. String type variable stores the name of the employee and Int type stores the Identity number of the employee. We have used other names for user-defined data types as, “employees” for “Array<Employee>” where “Employee” is a struct data type.
Swift
struct Employee
{
var employeeName: String
var employeeId: Int
init (employeeName: String , employeeId: Int )
{
self .employeeName = employeeName
self .employeeId = employeeId
}
}
class ComplexNumber
{
var realPart: Double
var imaginaryPart: Double
init (realPart: Double , imaginaryPart: Double )
{
self .realPart = realPart
self .imaginaryPart = imaginaryPart
}
}
typealias employees = Array <Employee>
var myArray: employees = []
var employee1 = Employee(employeeName: "Bhuwanesh" ,
employeeId: 131478)
var employee2 = Employee(employeeName: "Harshit" ,
employeeId: 256478)
var employee3 = Employee(employeeName: "Hitesh" ,
employeeId: 371948)
myArray.append(employee1)
myArray.append(employee2)
myArray.append(employee3)
for element in myArray
{
print ( "Employee Name :" , element.employeeName,
"," , "Employee Id :" , element.employeeId);
}
|
Output:
Employee Name : Bhuwanesh , Employee Id : 131478
Employee Name : Harshit , Employee Id : 256478
Employee Name : Hitesh , Employee Id : 371948
3. Type alias for complex data types
Complex data types are data types that consist of more than one primitive data type. We can specify type alias for complex data types also. For example, We can refer to (Int, Int) -> (Int) as “intIntInt”, (Int, Int) -> (Bool) as “intIntBool” etc. Like other data types, Internally, typealias doesn’t create new data types for existing complex data types. It just provides the alternate names for existing complex data types.
Syntax:
typealias myDataType = complexDataType
Here, complexDataType is one of the complex data-types, (Int) -> (Int), (Int) -> (Float), (Float) -> (Float), etc., myDataType is alias to dataType i.e, now we can call datatype with the name myDataType
Example:
In the below program, we are multiplying two integer numbers and we are assigning the defined function (To multiply two integers) to a complex data type. we have used another name or alias for complex data type as, “functionType” for “(Int, Int) -> Int”.
Swift
typealias functionType = ( Int , Int ) -> Int
func multiply( a: Int , b: Int ) -> Int {
return a * b
}
var myfunction: functionType = multiply
var myNumber1: Int = 7
var myNumber2: Int = 8
var answer = myfunction(myNumber1, myNumber2)
print ( "Multiplication of" , myNumber1,
"and" , myNumber2, "is" , answer)
|
Output:
Multiplication of 7 and 8 is 56
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...