Skip to content

Instantly share code, notes, and snippets.

Created June 4, 2014 05:03
Show Gist options
  • Save anonymous/5cc0af9ba0e94434d295 to your computer and use it in GitHub Desktop.
Save anonymous/5cc0af9ba0e94434d295 to your computer and use it in GitHub Desktop.
Possible Solution for Swift Enum conforming to a protocol
enum testEnum: ExampleProtocol{
case a, b, c
var simpleDescription: String{
get{
return "an example enum"
}
set {
simpleDescription = newValue
}
}
mutating func adjust() {
simpleDescription += " (whatever)"
}
}
var protoEnum = testEnum.a
let test = protoEnum.simpleDescription
@kasei
Copy link

kasei commented Jun 4, 2014

The error you're getting when you call adjust() is because the setter on simpleDescription is causing an infinite loop (the setter is trying to set the value it is responsible for). It's a strange example, but if you pull the underlying string out of the enum as a normal global variable, the getter/setter closures will use it just fine:

var description = "an example enum"
enum testEnum: ExampleProtocol{
    case a
    case b
    case c
    var simpleDescription: String {
        get{
            return description
        }
        set {
            description = newValue
        }
    }
    mutating func adjust() {
        simpleDescription += " (whatever)"
    }
}

var protoEnum = testEnum.a
protoEnum.adjust()
println(protoEnum.simpleDescription)
# prints: an example enum (whatever)

@kasei
Copy link

kasei commented Jun 4, 2014

Or, as a self-contained enumeration using associated values:

enum testEnum: ExampleProtocol{
    case a(String)
    case b(String)
    var simpleDescription: String {
        get{
            switch self {
            case a(let v):
                return "a: \(v)"
            case b(let v):
                return "b: \(v)"
            }
        }
        set {
            switch self {
            case a(let v):
                self = a(newValue)
            case b(let v):
                self = b(newValue)
            }
        }
    }
    mutating func adjust() {
        switch self {
        case a(let v):
            self = a(v + " (mutated)")
        case b(let v):
            self = b(v + " (mutated)")
        }
    }
}

var protoEnum = testEnum.b("test enum")
protoEnum.adjust()
println(protoEnum.simpleDescription)
# prints: b: test enum (mutated)

@MrSpockDe
Copy link

I am not exacly sure, how a conforming enum would look like.
I tried:

enum SimpleEnum: String {
        case simpleDescription = "A simple enum type."
        func adjust() ->String {
            return self.toRaw() + " - finally adjusted"
        }
    }

    var myEnum = SimpleEnum.simpleDescription
    let descr = myEnum.adjust()

because my understanding from reading the book was, that you only can use the expression after the collon as associated value. But probaby my solution is not conform to the protocol

@tempelmann
Copy link

Here's how I solved it based on kasei's code:

enum ServerResponse: ExampleProtocol {
    case Result(String, String)
    case Error(String)
    var simpleDescription: String {
        get {
            switch self {
            case let .Result(sunrise, sunset):
                return "Sunrise is at \(sunrise) and sunset is at \(sunset)."
            case let .Error(error):
                return "Failure...  \(error)"
            }
        }
    }
    mutating func adjust() {
        switch self {
            case Result(let v1, let v2):
                self = Result(v1 + " (adjusted)", v2 + " (adjusted)")
            case Error(let v1):
                self = Error(v1 + " (adjusted)")
        }
    }
}

var success = ServerResponse.Result("6:00 am", "8:09 pm")
var failure = ServerResponse.Error("Out of cheese.")
success.adjust()
failure.adjust()
print(success.simpleDescription)
print(failure.simpleDescription)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment