Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

dejo

Moderator emeritus
Original poster
Sep 2, 2004
15,982
452
The Centennial State
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.
 
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
 
Last edited:
I am still trying to understand but

Code:
    var simpleDescription: String {
        return "A simple enumeration"
    }

works
 
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
}
 
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.
 
Last edited:
From the documentation of Properties:

Computed properties are provided by classes, structures, and enumerations. Stored properties are provided only by classes and structures.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.