How to access a Swift enum associated value outside of a switch statement

后端 未结 5 1674
我寻月下人不归
我寻月下人不归 2021-01-31 07:08

Consider:

enum Line {
    case    Horizontal(CGFloat)
    case    Vertical(CGFloat)
}

let leftEdge             =  Line.Horizontal(0.0)
let leftMaskRightEdge             


        
5条回答
  •  旧时难觅i
    2021-01-31 07:51

    As others have pointed out, this is now kind of possible in Swift 2:

    import CoreGraphics
    
    enum Line {
        case    Horizontal(CGFloat)
        case    Vertical(CGFloat)
    }
    
    let min = Line.Horizontal(0.0)
    let mid = Line.Horizontal(0.5)
    let max = Line.Horizontal(1.0)
    
    func doToLine(line: Line) -> CGFloat? {
        if case .Horizontal(let value) = line {
            return value
        }
        return .None
    }
    
    doToLine(min) // prints 0
    doToLine(mid) // prints 0.5
    doToLine(max) // prints 1
    

提交回复
热议问题