Home »
Swift »
Swift Programs
Swift program to create the alias of a structure
Here, we are going to learn how to create the alias of a structure in Swift programming language?
Submitted by Nidhi, on July 10, 2021
Problem Solution:
Here, we will create structure and then create the alias name of created structure using the "typealias" keyword.
Program/Source Code:
The source code to create the alias of a structure is given below. The given program is compiled and executed successfully.
// Swift program to create an alias of a structure
import Swift
struct Person
{
var id: Int
var name: String
}
typealias Student = Person
var S = Student (id: 101, name: "virat")
print("Student id: \(S.id)" )
print("Student name: \(S.name)")
Output:
Student id: 101
Student name: virat
...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 a structure Person that contains two members id and name. Then we created the alias name Student of structure Person using the "typealias" keyword. After that, we created the object of structure using the Student alias and initialized and print the value of the structure.
Swift Typealias Programs »