Enums with prefixes, additional to string conversion method #10
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I was looking for a nice web service string enums workaround, and the best way so far was dealing with an enum and an array/dictionary. For example, there's an abstract service
makeUpName
, it has parameterage
, which is supposed to be one of strings: "Young", "Old", "Ancient". The old way is something like described here: http://stackoverflow.com/questions/1242914/converting-between-c-enum-and-xml or here: http://stackoverflow.com/questions/6331762/enum-values-to-nsstring-ios.The idea is to declare enum using JREnum, where all the enum elements have prefix equal to enum name, like:
And then, provide a function (makeUpNameAgeToStringCutPrefix() in my implementation), which could get enum element string representation without prefix, for example:
makeUpNameAgeYoung
=>Young
makeUpNameAgeOld
=>Old
etc. Wonder if some underscore additions could be implemented as well (prefix_enumName).
This approach is less verbose and reasonable if string constants don't change often. Autocomplete completes prefixes when enum elements are defined (didn't manage to create enum elements with prefix automatically, so one will have to type them by hand). Downside is that spaces are not supported and if string constants are often changed, the whole project has to be refactored (which is easy via XCode's rename feature).
I'm going to experiment with this for a while, what do you think?