The Swift documentation says that classes, structures, and enums can conform to the protocols, and I can get to the point where they all correspond. But I cannot make an enumeration behave like examples of classes and structures:
protocol ExampleProtocol { var simpleDescription: String { get set } mutating func adjust() } class SimpleClass: ExampleProtocol { var simpleDescription: String = "A very simple class." var anotherProperty: Int = 69105 func adjust() { simpleDescription += " Now 100% adjusted." } } var a = SimpleClass() a.adjust() let aDescription = a.simpleDescription struct SimpleStructure: ExampleProtocol { var simpleDescription: String = "A simple structure" mutating func adjust() { simpleDescription += " (adjusted)" } } var b = SimpleStructure() b.adjust() let bDescription = b.simpleDescription enum SimpleEnum: ExampleProtocol { case Base var simpleDescription: String { get { return "A Simple Enum" } set { newValue } } mutating func adjust() { self.simpleDescription += ", adjusted" } } var c = SimpleEnum.Base c.adjust() let cDescription = c.simpleDescription
I did not understand how to change simpleDescription as a result of calling adjust() . My example will obviously not do this because the recipient has a hard-coded value, but how can I set the value for simpleDescription still matching ExampleProtocol ?
enums swift swift-protocols
Adrian Harris Crowne Jun 03 '14 at 9:07 2014-06-03 09:07
source share