0

From swift 4 the protocol CaseIterable in enums has made my life happier but I would like to know if I can create an extension for an enum Type:String,CaseIterable.

So far I can create an enum String extension like that:

extension RawRepresentable where RawValue == String{

    func toCorrectFormatSring()->String{

        var returnedString = self.rawValue

        returnedString = returnedString.uppercased()

        return returnedString

    }
}

But I have some enums that have a common function and I don't want to repeat it in all the enums. The function gives all the cases in a coma separated string and it looks like that:

enum Vehicle:String,CaseIterable{

    case car
    case truck

    static func getStringList()->String{

        let aArray = self.allCases
        var returnedString = ""

        for aItem in aArray{

            returnedString += "\(aItem.toCorrectFormatSring())\(aItem == aArray.last ? "":",")"

         }

         return returnedString

    }

}

The function I want to use wit the extension is getStringList. Is it possible?

OUPUT

[CAR,TRUCK]
Reimond Hill
  • 4,278
  • 40
  • 52

1 Answers1

2

You probably want something like this:

extension RawRepresentable where RawValue == String {
     func toCorrectFormat() -> String {
        let returnedString = // whatever
        return returnedString
     }
}
extension CaseIterable where Self : RawRepresentable, Self.RawValue == String {
    static func getStringList() -> String {
        let aArray = Array(self.allCases)
        var returnedString = ""
        if let last = aArray.last {
            for aItem in aArray{
                returnedString += "\(aItem.toCorrectFormat())\(aItem == last ? "" : ",")"
            }
        }
        return returnedString
    }
}


Now you're good to go, because the protocol extension injects the desired static function into the enum:

enum E : String, CaseIterable {
    case howdy
    case byebye
}
let s = E.getStringList()

Now that you know how to inject the desired functionality, you can rewrite getStringList in a much better way (the loop is silly, the comparison with last is wrong, and the string interpolation is unnecessary). I think what you're really after is something like this:

extension CaseIterable where Self : RawRepresentable, Self.RawValue == String {
    static func getStringList() -> String {
        return Array(self.allCases)
            .map{$0.rawValue.uppercased()}
            .joined(separator:",")
    }
}
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • is my question written properly now? It is good if you tell me if something is not accurate and as a human I will rectify. It helps this forum improving and I appreciate all the answer as they make also this forum bigger of knowledge. I am aware you have responded some of mine in the past. If people correct their mistakes but stay with negative feedback they will be block of asking and this forum will have less participants. – Reimond Hill Oct 19 '18 at 11:24
  • No need to initialise a new array with allCases property. `return allCases .map` – Leo Dabus Nov 05 '18 at 23:23