Introduction
Swift is a programming language for developing apps for iOS, macOS, watchOS, and tvOS. Nonetheless, many aspects of Swift will be familiar to you if you've worked with C or Objective-C before.
There are many functionalities provided by swift, and one of them is - Nesting the different types in the type definition.
Let’s briefly learn about Nested types in Swift.
Nested Types In Swift
In Swift, Nested Types are types that can be used to nest other types in type definitions.
Enumerations are typically declared within a class or structure to support functionality, and we can nest supporting classes, structures, and enumerations within the specification of the type they support by utilizing nested types.
In enumerations, classes, and structures, Swift supports numerous nested types. We can define a nested class by defining one class and then declaring another class inside it, in the same way we can do with structures and enumerations.
Note: We don't have a standard syntax for nested types in Swift; instead, it's up to the programmer to declare it in their own method.
Nested Types Example
Here is an example of how to define nested type classes in Swift.
class Student {
var dept = Department()
class Department{
var StdId = 15;
var StdName = "Rajat Agrawal";
var StdSection = "10-A";
func fetchDetails() -> String {
return "StdId: \(self.StdId), StdName: \(self.StdName), StdSection: \(self.StdSection)"
}
}
}
var std = Student()
print(std.dept.fetchDetails())
You can see that we have defined a nested type class, the Department class is within the Student class and accessing the properties of nested class function values.
Output:

Output: