How to make an enum conform to a protocol in Swift?

前端 未结 15 1231
终归单人心
终归单人心 2020-12-22 16:32

Swift documentation says that classes, structs, and enums can all conform to protocols, and I can get to a point where they all conform. But I can

相关标签:
15条回答
  • 2020-12-22 17:17

    Here's another approach, using only the knowledge gained from the tour until that point*

    enum SimpleEnumeration: String, ExampleProtocol {
        case Basic = "A simple enumeration", Adjusted = "A simple enumeration (adjusted)"
    
        var simpleDescription: String {
            get {
                return self.toRaw()
            }
        }
    
        mutating func adjust() {
            self = .Adjusted
        }
    }
    
    var c = SimpleEnumeration.Basic
    c.adjust()
    let cDescription = c.simpleDescription
    

    If you want to have adjust() act as a toggle (although there's nothing to suggest this is the case), use:

    mutating func adjust() {
        switch self {
        case .Basic:
            self = .Adjusted
        default:
            self = .Basic
        }
    }
    

    *(Although it doesn't explicitly mention how to specify a return type and a protocol)

    0 讨论(0)
  • 2020-12-22 17:17

    Here's a solution that doesn't change the current enum value, but their instance values instead (just in case it is useful to anyone).

    enum ProtoEnumeration : ExampleProtocol {
        case One(String)
        case Two(String)
    
        var simpleDescription: String {
            get {
                switch self {
                case let .One(desc):
                    return desc
                case let .Two(desc):
                    return desc
                }
            }
        }
        mutating func adjust() {
            switch self {
            case let .One(desc):
                self = .One(desc + ", adjusted 1")
            case let .Two(desc):
                self = .Two(desc + ", adjusted 2")
            }
        }
    }
    
    var p = ProtoEnumeration.One("test")
    p.simpleDescription
    p.adjust()
    p.simpleDescription
    
    0 讨论(0)
  • 2020-12-22 17:17

    It is a link about enum in swift.

    Structures and enumerations are value types. By default, the properties of a value type cannot be modified from within its instance methods. link

    Then, you have to use mutating function.

    enum ProtocolEnum: ExampleProtocol {
        case on, off
        var simpleDescription: String {
            switch self {
            case .on:
                return "Switch is ON"
            case .off:
                return "Switch is OFF"
            }
        }
        mutating func adjust() {
            switch self {
            case .on:
                self = off
            case .off:
                self = on
            }
        }
    }
    
    var c = ProtocolEnum.on
    c.simpleDescription
    c.adjust()
    let cDescription = c.simpleDescription
    
    0 讨论(0)
  • 2020-12-22 17:18

    I came up with this

    protocol ExampleProtocol {
        var simpleDescription: String { get }
        mutating func adjust()
    }
    
    enum Seat: ExampleProtocol {
        case WindowSeat, MiddleSeat, AisleSeat
    
        var simpleDescription : String {
            switch self {
            case .WindowSeat:
                return "Window Seat"
            case .MiddleSeat:
                return "Middle Seat"
            case .AisleSeat:
                return "Aisle Seat"
            }
        }
    
        mutating func adjust() {
            switch self {
            case .WindowSeat:
                self = .MiddleSeat
            case .MiddleSeat:
                self = . AisleSeat
            case .AisleSeat:
                self = .WindowSeat
            }
        }
    }
    
    var seat = Seat.MiddleSeat
    print(seat.simpleDescription) // Middle Seat
    seat.adjust()
    print(seat.simpleDescription) // Aisle Seat
    
    0 讨论(0)
  • 2020-12-22 17:19

    It is not possible to define variables without getter and setter in enums and therefore it is impossible to have a variable that you can modify.

    You can conform to the protocol but you cannot have same behavior with mutating as in classes.

    0 讨论(0)
  • 2020-12-22 17:22

    here's my code

    enum SimpleEnum: ExampleProtocol {
        case Base, Adjusted
        var simpleDescription: String {
            get {
                var description = "A simple enum."
                switch self {
                case .Base:
                    return description
                case .Adjusted:
                    return description + " - [adjusted]"
                }
            }
        }
        mutating func adjust() {
            self = SimpleEnum.Adjusted
        }
    }
    var simpleEnum = SimpleEnum.Base
    simpleEnum.adjust()
    simpleEnum.simpleDescription
    
    0 讨论(0)
提交回复
热议问题