Understandign Swift - Type Aliases


What Are Type Aliases in Swift?

A type alias in Swift allows you to create an alternative name for an existing data type. It does not introduce a new type but provides a more readable and meaningful way to refer to an existing one.

For example, instead of writing Int, you can define typealias MyInt = Int and use MyInt throughout your program.

Why Use Type Aliases?

  • Improves code readability
  • Simplifies complex data types
  • Makes code more maintainable
  • Helps in abstraction

Syntax of Type Aliases in Swift

typeAlias NewName = ExistingType

A type alias can be applied to:

  • Primitive Data Types (Int, String, Float, etc.)
  • User-Defined Data Types (Array, Set, Dictionary, etc.)
  • Complex Data Types (Function types, Tuples, etc.)

Type Aliases for Primitive Data Types in Swift

Primitive data types include Int, Float, Double, String, and Character. By using type aliases, you can create custom names for these types.

Example: Creating Type Aliases for Primitive Data Types

import Foundation

// Creating type aliases for primitive data types
typealias MyString = String
typealias MyNumber = Float
typealias Num = Int

// Declaring variables using type aliases
var number: Num = 10
var newString: MyString = "Swift Programming"
var value: MyNumber = 23.456

// Printing values and types
print("Number:", number, "| Type:", type(of: number))
print("String:", newString, "| Type:", type(of: newString))
print("Float:", value, "| Type:", type(of: value))

Output

Number: 10 | Type: Int
String: Swift Programming | Type: String
Float: 23.456 | Type: Float

Type Aliases for User-Defined Data Types in Swift

Type aliases can also be applied to collections like Array, Set, and Dictionary.

Example: Using Type Aliases for Collections

import Foundation

// Creating type aliases for collections
typealias MySet = Set<Int>
typealias MyArray = Array<Int>
typealias StringArray = Array<String>

// Initializing collections using aliases
var numbersSet: MySet = [32, 3, 1, 2]
var numbersArray: MyArray = [32, 2, 1, 1, 3]
var languages: StringArray = ["Swift", "C++", "C#"]

// Printing results
print("Set:", numbersSet)
print("Array of Int:", numbersArray)
print("Array of Strings:", languages)

Output

Set: [32, 3, 1, 2]
Array of Int: [32, 2, 1, 1, 3]
Array of Strings: ["Swift", "C++", "C#"]

Type Aliases for Complex Data Types in Swift

Type aliases can be used for function types, tuples, and closures, making it easier to handle complex function signatures.

Example: Using Type Aliases for Function Types

import Foundation

// Creating a type alias for a function type
typealias StringConcatenation = (String, String) -> String

// Function that concatenates two strings
func concatenate(s1: String, s2: String) -> String {
return s1 + " " + s2
}

// Assigning function to type alias
var newFunc: StringConcatenation = concatenate

// Using the alias function
var result = newFunc("Hello", "World")

print(result)

Output

Hello World