Lets say I have this enum defined (from Apple's UIKit framework):
typedef enum {
UITextAutocorrectionTypeDefault,
UITextAutocorrectionTypeNo,
UITextAutocorrectionTypeYes,
} UITextAutocorrectionType;
and I have a simple string with "UITextAutocorrectionTypeDefault" value. In my case, this string is coming from a JSON object, but it doesn't matter.
What would be the best way for me to convert the string into the proper enum value so I can set a property in the class? I know Obj-C can't just convert the value directly (like you can do with Java or C#), and in my case I have lots of enums, and ones that may be created at a later time. Are there any automatic or semi-automatic ways of doing this, other than having to create a mapping dictionary manually?
In C (read Objective-C) there is no way to do this other than manually mapping the values. The enum just defines constant values that can not be converted to or from a string at runtime like they can in C# or Java which treats them as objects.
You can how ever stringify the values at compile time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With