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

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

Consider:

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

let leftEdge             =  Line.Horizontal(0.0)
let leftMaskRightEdge             


        
5条回答
  •  有刺的猬
    2021-01-31 07:56

    You can use a guard statement to access the associated value, like this.

    enum Line {
        case    Horizontal(Float)
        case    Vertical(Float)
    }
    
    let leftEdge             =  Line.Horizontal(0.0)
    let leftMaskRightEdge    =  Line.Horizontal(0.05)
    
    guard case .Horizontal(let leftEdgeValue) = leftEdge else { fatalError() }
    
    print(leftEdgeValue)
    

提交回复
热议问题