1、类型嵌套
Swift 支持类型嵌套,把需要嵌套的类型的定义写在被嵌套的类型的
{}
中。Swift 中的枚举类型可以辅助实现特定的类或者结构体的功能。
struct SchoolUniform { enum Style: String { // 在结构体中嵌套枚举 case sports = "yundongfu" case suit = "zhongshanzhuang" } enum Grade: String { // 在结构体中嵌套枚举 case one = "chuyi" case two = "chuer" case three = "chusan" } let myStyle: Style let myGrade: Grade func customize() { print("my grade: \(myGrade), my style: \(myStyle)") } }
let uniform4XiaoMing = SchoolUniform(myStyle: .suit, myGrade: .one) uniform4XiaoMing.customize() // my grade: one, my style: suit