Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

have the compiler add an NSDictionary for every enum type #49

Open
headsupftw opened this issue May 29, 2015 · 2 comments
Open

have the compiler add an NSDictionary for every enum type #49

headsupftw opened this issue May 29, 2015 · 2 comments

Comments

@headsupftw
Copy link

Even though Objective-C is a dynamic language, it's basically impossible to get an enum value given its name as an NSString because an Objective-C/C enum is essentially just an integer (whereas in Java, it can easily be done via the reflection API).

But in the context of protobuf, this problem can be solved by having the compiler add an NSDictionary in which the keys are enum names (NSString) and values are enum values. It could add another dictionary to map enums to their names.

@alexeyxo
Copy link
Owner

I don't see the meaning...
Can you explain or give an example?

@headsupftw
Copy link
Author

Sure, this is an enhancement request BTW.

Below is a snippet from a file generated by protobuf compiler.

typedef NS_ENUM(SInt32, MessageType) {
MessageTypeTimeout = 0,
MessageTypeDisconnect = 1,
MessageTypeCheckin = 2,
};

Now in my source code I already have NSString such as @"Timeout" and @"Disconnect", which I would like to convert to a MessageType enum. I.e. @"Timeout" -> MessageTypeTimeout and @"Disconnect" -> MessageTypeDisconnect.

There's no easy way to do that in Objective-C. But since the enum type was generated by the compiler, it would be nice if the compiler also inserted a NSDictionary in the file. For example,

NSDictionary *messageTypeDict = @{
@"MessageTypeTimeout" : @(MessageTypeTimeout),
@"MessageTypeDisconnect" : @(MessageTypeDisconnect),
@"MessageTypeCheckin" : @(MessageTypeCheckin)
};

It would be even nicer to have the opposite dictionary inserted as well.

NSDictionary *messageTypeStringDict = @{
@(MessageTypeTimeout) : @"MessageTypeTimeout",
@(MessageTypeDisconnect) : @"MessageTypeDisconnect",
@(MessageTypeCheckin) : @"MessageTypeCheckin"
};

The whole point is to provide a way to inspect every enum type, which is something not possible in Objective-C but doable with the help of protobuf compiler.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants