Swift: ExampleProtocol-conforming enumeration

Discussion in 'Mac Programming' started by dejo, Jun 3, 2014.

  1. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #1
    Anybody else trying the experiments in the GuidedTour Playground? I am. But I'm kinda stuck on one near the end where you are asked to "Write an enumeration that conforms to this protocol." "This protocol" being the ExampleProtocol shown earlier, seen here:
    Code:
    protocol ExampleProtocol {
         var simpleDescription: String { get }
         mutating func adjust()
    }
    I tried this:
    Code:
    enum SimpleEnum: ExampleProtocol {
        case Enum1, Enum2
        [COLOR="Red"][B]var simpleDescription: String = "A simple enumeration"[/B][/COLOR]
        mutating func adjust() {
            simpleDescription += " (adjusted)"
        }
    }
    but get this error, on the line highlighted above:
    Code:
    'var' declarations without getter/setter not allowed here
    So, I know I'll need something like:
    Code:
    var simpleDescription: String {
            get {
                // ???
            }
            set {
                // ???
            }
        }
    but I'm not sure what I need to put in the getter and setter to allow it to start with "A simple enumeration" but allow it to be changed later, such as via the adjust() function.

    Any hints to get me headed in the right direction? Thanks in advance.
     
  2. ridman, Jun 3, 2014
    Last edited: Jun 3, 2014

    ridman macrumors newbie

    Joined:
    Jun 3, 2014
    #2
    Hi dejo

    try something like this:


    Code:
    enum SimpleEnum: ExampleProtocol {
        case Enum1, Enum2
        var simpleDescription: String {
        get {
            switch self {
            case .Enum1:
                return "Enum 1."
            case .Enum2:
                return "Enum 2."
            }
        }
        set {
            simpleDescription = newValue
        }
        }
        mutating func adjust() {
            simpleDescription += " (adjusted)"
        }
    }
    Although, I tried calling the -adjust() method and it didn't seem to append the " (adjusted)" string. If anyone knows a way to fix that, I'd be interested to hear it. Otherwise, I'm moving on to the next chapter. Thanks
     
  3. JohnsonK macrumors regular

    Joined:
    Mar 6, 2014
    #3
    I am still trying to understand but

    Code:
        var simpleDescription: String {
            return "A simple enumeration"
        }
    
    works
     
  4. dejo thread starter Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    But that won't work with in conduction with the adjust() function.
     
  5. bsolar macrumors 6502a

    Joined:
    Jun 20, 2011
    #5
    It seems to me that the key issue is whether enums can actually have stored properties: the error message requiring a getter seems to imply that the use of a computed property is mandatory. In the example above the getter does not read any state on the enum, only which enum it is, and I'm not sure the setter actually works.

    A function like this technically complies with the protocol, but doesn't try to change stored state:

    Code:
    mutating func adjust() {
        self = Enum2
    }
     
  6. bsolar, Jun 3, 2014
    Last edited: Jun 3, 2014

    bsolar macrumors 6502a

    Joined:
    Jun 20, 2011
    #6
    Another take at the problem, this time using a single enumeration member with associated value:

    Code:
    enum EnumWithText: ExampleProtocol {
        
        case Enum1(String)
        
        var simpleDescription: String {
            get {
                switch self {
                case let .Enum1(text):
                    return text
                }
            }
            set {
                switch self {
                case let .Enum1(text):
                    self = .Enum1(newValue)
                }
            }
        }
        
        mutating func adjust() {
            switch self {
            case let .Enum1(text):
                self = .Enum1(text + " (adjusted)")
            }
        }
        
    }
    
    var e = EnumWithText.Enum1("Hello")
    var text = e.simpleDescription
    e.simpleDescription = "Adios"
    text = e.simpleDescription
    e.adjust()
    text = e.simpleDescription
    Again, technically the enum member is immutable: the setter and the adjust function create a new enum member and set self to it.
     
  7. bsolar macrumors 6502a

    Joined:
    Jun 20, 2011
    #7
    From the documentation of Properties:

     
  8. dejo thread starter Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    Thanks, bsolar. So, it seems that setting a new enum is probably the way to go.
     

Share This Page