Created
April 3, 2015 10:25
-
-
Save boylee1111/ff2ad3dcb0a69b92adb4 to your computer and use it in GitHub Desktop.
A Swift Tour. Write an enumeration that conforms to this protocol.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol ExampleProtocol { | |
var simpleDescription: String { get } | |
mutating func adjust() | |
} | |
enum SimpleEnum : String, ExampleProtocol { | |
case Before = "", After = " (whatever)" | |
var simpleDescription: String { | |
get { | |
return "A simple enum." + self.rawValue | |
} | |
} | |
mutating func adjust() { | |
self = .After | |
} | |
} | |
var c = SimpleEnum.Before | |
c.adjust() | |
c.simpleDescription |
Hi, I typed your code but Xcode(8.3.2) gave me a compiler error for raw type error. The codes in the following are modified according to your codes.
enum SimpleEnumeration: ExampleProtocol {
case Before, After
var simpleDescription: String {
get {
return "A simple enum."
}
}
mutating func adjust() {
self = .After
}
}
var c = SimpleEnumeration.Before
a.adjust()
c.simpleDescription
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just googled for the exercise and found this.
Your solution modifies the enum's value. Which is just fine, but I wanted to show others an alternative:
Based on this gist I came up with this which keeps the Enum value and instead changes its associated values: