关于ios:Swift-Learning-Summary-Nested-Types

70次阅读

共计 731 个字符,预计需要花费 2 分钟才能阅读完成。

Nested Types

Enumerations, classes or structures can be nested in another type.

struct Closh {
    enum Size: String{case H = "high", M = "Medium", L = "Low"}
    
    enum Detail: Int {
        case H = 180, M = 170, L = 160
        struct Price {let normal: Int, discount: Int?}
        var price: Price {
            switch self {
            case .H:
                return Price(normal: 100, discount: 90)
            case .M:
                return Price(normal: 90, discount: 80)
            case .L:
                return Price(normal: 80, discount: 70)
            default:
                return Price(normal: self.rawValue, discount: nil)
            }
        }
    }
    
    let size: Size, detail: Detail
    var description: String {return "\(size.rawValue), PriceNormal: \(detail.price.normal) PriceDiscount: \(detail.price.discount)"  
    }
}

var closh = Closh(size: .M, detail: .M)
print(closh.description)
// Print: Medium, PriceNormal: 90 PriceDiscount: Optional(80)

Referring to Nested Types

var L = Closh.Size.L.rawValue
print(L)     // Low

正文完
 0