Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Swift – Typealias

  • Last Updated : 14 Feb, 2022

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




// Swift program to illustrate typealias
// for primitive data types
 
// myInt is an alias to Int
// After this statement using Int or
// myInt is equivalent
typealias myInt = Int
 
// myFloat is an alias to Float
// After this statement using Float or
// myFloat is equivalent
typealias myFloat = Float
 
// myDouble is an alias to Double
// After this statement using Double or
// myDouble is equivalent
typealias myDouble = Double
 
 
// myCharacter is an alias to Character
// After this statement using Character or
// myCharacter is equivalent
typealias myCharacter = Character
 
// myDouble is an alias to String
// After this statement using String or
// myString is equivalent
typealias myString = String
 
// Declaring integerNumber of Int type
// myInt is an alias for Int
var integerNumber : myInt
 
// Initializing the variable
integerNumber = 5
 
// Print the value represented by integerNumber
print("integerNumber:", integerNumber)
 
// Declaring floatNumber of Float type
// myFloat is an alias for Float
var floatNumber : myFloat
 
// Initializing the variable
floatNumber = 5.12345
 
// Print the value represented by floatNumber
print("floatNumber:", floatNumber)
 
// Declaring doubleNumber of Double type
// myDouble is an alias for Double
var doubleNumber : myDouble
 
// Initializing the variable
doubleNumber = 5.123456789
 
// Print the value represented by doubleNumber
print("doubleNumber:", doubleNumber)
 
// Declaring character of Character type
// myCharacter is an alias for Character
var character : myCharacter
 
// Initializing the variable
character = "A"
 
// Print the value represented by character
print("character:", character)
 
// Declaring character of Int type
// myInt is an alias for Int
var string : myString
 
// Initializing the variable
string = "GeeksforGeeks"
 
// Print the value represented by string
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




// Swift program to illustrate typealias for
// user-defined and derived data types
 
// Defining a structure
struct Employee
{
 
    // Declaring a variable of string type
    var employeeName: String
         
    // Declaring a variable of int type
    var employeeId: Int
   
    // Defining a custom constructor
    init(employeeName: String, employeeId: Int)
    {
       
        // Initializing data members with the parameters
        // passed to the constructor
        self.employeeName = employeeName
        self.employeeId = employeeId
   }
 
}
 
// Defining a class
class ComplexNumber
{
    var realPart: Double
    var imaginaryPart: Double
     
    init(realPart: Double, imaginaryPart: Double)
    {
        self.realPart = realPart
        self.imaginaryPart = imaginaryPart
    }
}
 
// Defining alias to "Array<Employee>"
// After this statement using employees or
// Array<Employee> is equivalent
typealias employees = Array<Employee>
 
// myArray is an array of type Employee
var myArray: employees = []
 
// Instantiate an object of structure
var employee1 = Employee(employeeName: "Bhuwanesh",
                         employeeId: 131478)
 
// Instantiate another object of structure
var employee2 = Employee(employeeName: "Harshit",
                         employeeId: 256478)
 
// Instantiate another object of structure
var employee3 = Employee(employeeName: "Hitesh",
                         employeeId: 371948)
 
// Append Employee objects in the array
myArray.append(employee1)
myArray.append(employee2)
myArray.append(employee3)
 
// Print array elements
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




// Swift program to illustrate type alias for complex types
 
// Alias to function type (Int, Int) -> Int
// After this statement using (Int, Int) -> Int or
// functionType is equivalent
typealias functionType = (Int, Int) -> Int
 
// Function to multiply variables
func multiply( a: Int, b: Int) -> Int {
    return a * b
}
 
// Assigning multiply function to myFunction
var myfunction: functionType = multiply
 
// Initializing variables
var myNumber1: Int = 7
var myNumber2: Int = 8
 
// Calling function through variable
var answer = myfunction(myNumber1, myNumber2)
 
// Print the value represented by the answer
print("Multiplication of", myNumber1,
      "and", myNumber2, "is", answer)

Output:

Multiplication of 7 and 8 is 56

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!