你能只扩展使用字符串作为原始值类型的 RawRepresentables 吗?

Can you extend only RawRepresentables which use Strings for the raw value type?

我正在尝试编写一个基于字符串扩展枚举的扩展。我知道扩展所有枚举的方法是扩展 RawRepresentable,但我希望它仅限于字符串。

extension RawRepresentable where RawRepresentable.RawValue == String{

    func foo(){

        let myRawValue:String = self.rawValue

    }
}

那么如何指定 'where' 子句来实现这一点?

要仅基于 String 扩展 RawRepresentablewhere 子句就是 where RawValue == String:

extension RawRepresentable where RawValue == String {

    func foo() {
        let myRawValue:String = self.rawValue
        print(myRawValue)
    }
}


enum Flintstone: String {
    case fred, wilma, pebbles
}

Flintstone.fred.foo()  // fred