Q:

Swift program to create the alias of a structure

belongs to collection: Swift Typealias Programs

0

Here, we will create structure and then create the alias name of created structure using the "typealias" keyword.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
<< Swift program to create typealias for built-in typ...