Joey Joey - 3 months ago 23
Swift Question

Access String value in enum without using rawValue

I would like to replace my global string constants with a nested enum for the keys I'm using to access columns in a database.

The structure is as follows:

enum DatabaseKeys {

enum User: String {
case Table = "User"
case Username = "username"



Each table in the database is an inner enum, with the name of the table being the enum's title. The first case in each enum will be the name of the table, and the following cases are the columns in its table.

To use this, it's pretty simple:

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

But I will be using these enums a lot. Having to append
to every instance will be a pain, and it's not as readable as I'd like it to be. How can I access the String value without having to use
? It'd be great if I can do this:

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

Note that I'm using Swift 2. If there's an even better way to accomplish this I'd love to hear it!


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.