Access String value in enum without using rawValue

You can use callAsFunction (New in Swift 5.2) on your enum that conforms to String.

enum KeychainKey: String {
   case userId
   case email
}

func callAsFunction() -> String {
    return self.rawValue
}

usage:

KeychainKey.userId()

You can use CustomStringConvertible protocol for this.

From documentation,

String(instance) will work for an instance of any type, returning its description if the instance happens to be CustomStringConvertible. Using CustomStringConvertible as a generic constraint, or accessing a conforming type's description directly, is therefore discouraged.

So, if you conform to this protocol and return your rawValue through the description method, you will be able to use String(Table.User) to get the value.

enum User: String, CustomStringConvertible {

    case Table = "User"
    case Username = "username"

    var description: String {
        return self.rawValue
    }
}

var myUser = [String: String]()
myUser[String(DatabaseKeys.User.Username)] = "Johnny"

print(myUser) // ["username": "Johnny"]

While I didn't find a way to do this using the desired syntax with enums, this is possible using structs.

struct DatabaseKeys {

    struct User {
        static let identifier = "User"
        static let Username = "username"
    }

}

To use:

myUser[DatabaseKeys.User.Username] = "Johnny"

Apple uses structs like this for storyboard and row type identifiers in the WatchKit templates.