Swift program to create typealias for built-in types

Here, we are going to learn how to create typealias for built-in types in Swift programming language?
Submitted by Nidhi, on July 10, 2021

Problem Solution:

Here, we will create an alias of built types using the "typealias" keyword. Then we will create variables using created alias.

Program/Source Code:

The source code to create typealias for built-in types is given below. The given program is compiled and executed successfully.

// Swift program to demonstrate typealias

import Swift

typealias MyString = String
typealias MyInt    = Int
typealias MyDouble = Double
typealias MyBoolean = Bool

var name:MyString  = "Rohit Kohli"
var num1:MyInt     = 10
var num2:MyDouble  = 10.35
var flag:MyBoolean = true

print(name)
print(num1)
print(num2)
print(flag)

Output:

Rohit Kohli
10
10.35
True

...Program finished with exit code 0
Press ENTER to exit console.

Explanation:

In the above program, we imported a package Swift to use the print() function using the below statement,

import Swift

Here, we created alias names for String, Int, Double, Bool types using the typealias keyword and then created variable name, num1, num2, flag variables using created alias. After that, we printed the value of variables on the console screen.

Swift Typealias Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.