Table of contents
1.
Introduction
2.
Nested Types In Swift
2.1.
Nested Types Example
3.
Swift Access Nested Types
4.
Frequently asked questions
4.1.
What is Swift?
4.2.
What are Nested types in Swift?
4.3.
Is there any standard syntax for writing nested types in Swift?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Swift Nested Types

Author Rajat Agrawal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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:

Swift Access Nested Types

If we want to access the Nested Types outside of the definition, we must use their name together with a nested type.

Let’s understand the above statement with an example.

Here is an example of defining and accessing nested types in Swift.

class Student {

enum StudentRemarks {
case Topper
case Average
case Poor
}

func remark() -> StudentRemarks {
return .Topper
}
}

class Teacher {
let student = Student()
var remarks: Student.StudentRemarks
init() {
self.remarks = self.student.remark()
}
}
var res1 = Student.StudentRemarks.Topper
var res2 = Student.StudentRemarks.Average
print(res1)
print(res2)

You can see that we defined a StudentRemarks class as a nested class for the Student class, and we are accessing it by creating an instance of the Student class and calling the enumeration in class to get the desired output.

Output:

Frequently asked questions

What is Swift?

Swift is a programming language for developing apps for iOS, macOS, watchOS, and tvOS. 

What are Nested types in Swift?

In Swift, Nested Types are types that can be used to nest other types in type definitions.

Is there any standard syntax for writing nested types in Swift?

No, 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 way.

Conclusion

In this article, we have extensively discussed the Nested Types in Swift, nesting different types in the type definition, and a few code examples. If you want to learn more, check out our blogs on Swift InitializationSwift DeinitializationSwift GenericsSwift ProtocolsTimers In Swift, and Swift Higher Order Function.

Do upvote our blog to help other ninjas grow.

Happy Coding!

Live masterclass