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

Enum Constants Explanation #2081

Merged
merged 2 commits into from
Jul 22, 2015
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions docs/NativeModulesIOS.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,43 @@ console.log(CalendarManager.firstDayOfTheWeek);

Note that the constants are exported only at initialization time, so if you change `constantsToExport` values at runtime it won't affect the JavaScript environment.

###Enum Constants
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: space between the ### and the E


Enums that are defined via `NS_ENUM` cannot be used as method arguments without first extending RCTConvert.

In order to export the following `NS_ENUM` definition:
```objc
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: add newline between 237 and 238 here

typedef NS_ENUM(NSInteger, MyEnumType)
{
kEnumOne,
kEnumTwo,
};
```

You must create a class extension of RCTConvert like so:
```objc
@implementation RCTConvert (MyEnumExtension)
RCT_ENUM_CONVERTER(MyEnumType, (@{ @"enumOne" : @(kEnumOne),
@"enumTwo" : @(kEnumTwo),
kEnumOne, integerValue)
@end
```

You can then define methods and export your enum constants like this:

```objc
- (NSDictionary *)constantsToExport
{
return @{ @"enumOne" : @(kEnumOne),
@"enumTwo" : @(kEnumTwo) }
};

RCT_EXPORT_METHOD(doSomethingWithEnum:(MyEnumType)enum
completion:(RCTResponseSenderBlock)callback)
```

Your enum will then be automatically unwrapped using the selector provided (`integerValue` in the above example) before being passed to your exported method.


## Sending Events to JavaScript

Expand Down