I would like to replace my global string constants with a nested enumeration for the keys that I use to access the columns in the database.
The structure is as follows:
enum DatabaseKeys { enum User: String { case Table = "User" case Username = "username" ... } ... }
Each table in the database is an internal enumeration, and the table name is an enumeration heading. The first case in each enumeration will be the name of the table, and the following cases will be the columns in its table.
To use this, it is quite simple:
myUser[DatabaseKeys.User.Username.rawValue] = "Johnny"
But I will use these listings a lot . There will be pain in adding .rawValue to each instance, and it is not as readable as we would like. How can I access a string value without using rawValue ? It would be great if I can do this:
myUser[DatabaseKeys.User.Username] = "Johnny"
Notice that I'm using Swift 2. If there is an even better way to do this, I'd love to hear it!
string enums swift swift2
Joey
source share