Swift - 嵌套类型



嵌套类型是 Swift 编程中一个非常强大的工具。使用嵌套类型,我们可以轻松地将枚举、类和结构体嵌套到其他枚举、类和结构体中。使用嵌套类型,我们可以将枚举或类嵌套到结构体中,反之亦然。所以我们可以说,使用嵌套类型可以创建复杂的自定义类型。

嵌套类型通常用于创建组织良好且分层结构的程序。它们也用于封装代码,以便我们更容易理解。它们还可以减少命名空间冲突。

创建嵌套类型

我们可以通过将一个结构体定义到另一个结构体中,或者将一个类定义到另一个类中来创建嵌套类型,就像嵌套函数一样。我们可以在另一个结构体、类和枚举中创建多个结构体、类和枚举。

语法

以下是嵌套类型的语法:

Struct Name{
   Struct name{
      // body
   }
}

使用此语法,我们可以嵌套类和枚举。

示例

假设我们正在创建一个汽车的结构体,其中包含多种类型的特性,例如颜色和天窗。因此,使用嵌套类型,我们可以定义每种类型的规格。

struct Car{
   struct Color{
      var single: String
      var double: String
      var customize: String
   }
   struct Sunroof{
      var half: String
      var full: String
   }
}

创建嵌套类型的实例

我们可以使用点运算符创建嵌套类型的实例。只需在外部类型的名称前加上点运算符,然后指定嵌套类型的名称即可。每个嵌套类型都有其实例。

语法

以下是创建嵌套类型实例的语法:

let name = outerTypeName.nestedTypeName(value: type)

或者:

var name = OuterTypeName(label1:Value, lable2: 
OuterTypeName.NestedTypeName(label: Value, lable2: Value))

示例

让我们创建上述嵌套类型的实例:

let instance1 = Car.Color(single: "Yellow", double: "Green Black", customize: "Blue")
let instance2 = Car.Sunroof(half: "yes", full: "No")

或者:

var myCar = Car(name:"Breeza", color: Car.Color(
single: "Blue", double: "No", customize: "White"), 
sunroof:Car.Sunroof(half: "Yes", full: "No"))

完整的示例如下:

import Foundation

// Outer sturtcure
struct Car{

   // Nested structure 1
   struct Color{
      var single: String
      var double: String
      var customize: String
   }
   // Nested structure 1
   struct Sunroof{
      var half: String
      var full: String
   }
    
   // Properties
   var name: String
   var color: Color
   var sunroof:Sunroof
}

// Creating instance of Car structure
var myCar = Car(name:"Breeza", color: Car.Color(single: "Blue", double: "No", customize: "White"), sunroof:Car.Sunroof(half: "Yes", full: "No"))

// Displaying Data
print("Car name:", myCar.name)
print("Car Color:", myCar.color.single)
print("Have half Sunroof:", myCar.sunroof.half)

输出

它将产生以下输出:

Car name: Breeza
Car Color: Blue
Have half Sunroof: Yes

结构体内的枚举

使用嵌套类型,您不必只将结构体嵌套到另一个结构体中。我们还可以将枚举嵌套到另一个结构体中,反之亦然。

示例

将枚举嵌套到结构体中的 Swift 程序。

import Foundation

// Outer structure
struct Tutorialspoint {

   // Nested enum
   enum Department {
      case HR
      case Development
      case Designing
      case ContentWriting
   }
    
   // Properties
   var name: String
   var location: String
   var department: Department
    
   init(name: String, location: String, department: Department) {
      self.name = name
      self.location = location
      self.department = department
   }
    
   // Method
   func employeeDetail() {
      print("Name: \(name)")
      print("Location: \(location)")
      print("Department: \(department)")
   }
}

let report = Tutorialspoint(name: "Mona", location: "Hyderabad", department: .Designing)
report.employeeDetail()

输出

它将产生以下输出:

Name: Mona	
Location: Hyderabad
Department: Designing

类内的结构体

使用嵌套类型,我们可以将结构体嵌套到类中,或者将类嵌套到结构体中,而不会出现任何错误。

示例

将结构体嵌套到另一个类中的 Swift 程序。

import Foundation

// Outer class
class XYZCollege {

   // Nested structure
   struct StudentDetail {
      var name: String
      var age: Int
      var subject: String
      var year: Int
   }
    
   // Properties
   var department: String
   var details: [StudentDetail]
    
   init(department: String) {
      self.department = department
      self.details = []
   }
    
   // Method 1
   func newStudents(student: StudentDetail) {
      details.append(student)
   }
    
   // Method 2
   func studentsData() {
      print("department name: \(department):")
      for s in details {
         print("name: \(s.name), age: \(s.age), subject: \(s.subject), year: \(s.year)")
      }
   }
}

// Creating instances
let s = XYZCollege(department: "Science")
let stud1Detail = XYZCollege.StudentDetail(name: "Mona", age: 19, subject: "Maths", year: 1)
let stud2Detail = XYZCollege.StudentDetail(name: "Pihu", age: 18, subject: "Chemistry", year: 1)
let stud3Detail = XYZCollege.StudentDetail(name: "Suman", age: 19, subject: "Maths", year: 1)

// Accessing the methods
s.newStudents(student: stud1Detail)
s.newStudents(student: stud2Detail)
s.newStudents(student: stud3Detail)

s.studentsData()

输出

它将产生以下输出:

department name: Science:
name: Mona, age: 19, subject: Maths, year: 1
name: Pihu, age: 18, subject: Chemistry, year: 1
name: Suman, age: 19, subject: Maths, year: 1
广告