Swift Enumeration order and comparison

后端 未结 4 841
故里飘歌
故里飘歌 2021-02-18 15:59

I\'ve had trouble finding/understanding documentation on how to compare enums in Swift by their order of definition. Specifically when I create an enumeration such as



        
4条回答
  •  失恋的感觉
    2021-02-18 16:05

    Comparing enums as the OP wanted will be possible from Swift 5.3 onwards.

    It works as below (from proposal):

    enum Brightness: Comparable {
        case low
        case medium
        case high
    }
    
    let expectedBrightness = Brightness.low
    let actualBrightness = Brightness.high
    
    if actualBrightness > expectedBrightness {
        // Do something
    }
    

    More info and examples here.

提交回复
热议问题