Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an enum conform to a protocol in Swift?

Swift documentation says that classes, structs, and enums can all conform to protocols, and I can get to a point where they all conform. But I can't get the enum to behave quite like the class and struct examples:

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 haven't figured out how to get the simpleDescription to change as a result of calling adjust(). My example obviously won't do that because the getter has a value hard-coded, but how can I set a value for the simpleDescription while still conforming to the ExampleProtocol?

like image 754
Adrian Harris Crowne Avatar asked Jun 03 '14 09:06

Adrian Harris Crowne


People also ask

Can enum conform to Swift protocol?

Yes, enums can conform protocols. You can use Swift's own protocols or custom protocols. By using protocols with Enums you can add more capabilities.

Can an enum have a function Swift?

Enumerations (or enums for short) in Swift define a common type for a group of related values. According to the Swift documentation, enums enable you to work with those values in a type-safe way within your code. Enums come in particularly handy when you have a lot of different options you want to encode.

How does enum work in Swift?

What is a Swift Enum? According to the Swift documentation enumeration is defined as “a common type for a group of related values and enables you to work with those values in a type-safe way within your code”. Think of it as a type of variable that is specifically used switch/conditionals.

Can enums inherit Swift?

In Swift language, we have Structs, Enum and Classes. Struct and Enum are passed by copy but Classes are passed by reference. Only Classes support inheritance, Enum and Struct don't.


2 Answers

This is my attempt:

protocol ExampleProtocol {     var simpleDescription: String { get }     mutating func adjust() }  enum ExampleEnum : ExampleProtocol {     case Base, Adjusted      var simpleDescription: String {         return self.getDescription()     }      func getDescription() -> String {         switch self {         case .Base:             return "A simple description of enum"         case .Adjusted:             return "Adjusted description of enum"         }     }      mutating func adjust() {         self = ExampleEnum.Adjusted     } }  var c = ExampleEnum.Base c.adjust() let cDescription = c.simpleDescription 
like image 173
Hu Qiang Avatar answered Sep 30 '22 06:09

Hu Qiang


Here is my take at it.

As this is an enum and not a class, you have to think different(TM): it is your description that has to change when the "state" of your enum changes (as pointed out by @hu-qiang).

enum SimpleEnumeration: ExampleProtocol {   case Basic, Adjusted    var description: String {     switch self {     case .Basic:       return "A simple Enumeration"     case .Adjusted:       return "A simple Enumeration [adjusted]"     }   }    mutating func adjust()  {     self = .Adjusted   } }  var c = SimpleEnumeration.Basic c.description c.adjust() c.description 

Hope that helps.

like image 26
Zedenem Avatar answered Sep 30 '22 06:09

Zedenem