Conversation
@@ -322,6 +323,25 @@ typedef NS_ENUM(NSInteger, NYTAdRequestState) { | |||
}; | |||
``` | |||
|
|||
## Boxing Enumerated Types | |||
|
|||
Althought `enum`s are integral values, they cannot be directly used as [boxed literals](http://clang.llvm.org/docs/ObjectiveCLiterals.html#boxed-enums). So when creating a dictionary using `enum`s, they should be written as boxed literals instead of their corresponding `NSNumber` expression. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo: "Althought"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would write the second sentence…
When boxing
enum
s (for example, when using them in a Cocoa collection) they should be boxed asNSNumber
s using the shorthand@()
literal expression, not as the[NSNumber numberWith…:]
methods.This eliminates the need to look up the underlying type of the enum in order to box it correctly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think "integral" isn't the right word. I would say "integer".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, my dictionary fail
@@ -322,6 +323,27 @@ typedef NS_ENUM(NSInteger, NYTAdRequestState) { | |||
}; | |||
``` | |||
|
|||
## Boxing Enumerated Types | |||
|
|||
Although `enum`s are integral values, they cannot be directly used as [boxed literals](http://clang.llvm.org/docs/ObjectiveCLiterals.html#boxed-enums). When boxing `enum`s (for example when using them in a Cocoa collection) they should be boxed as `NSNumber`s using the shorthand `@()` literal expression, not as the [NSNumber numberWith...:]` methods. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
…, not as the [NSNumber numberWith...:]` methods.
You're missing the backtick before "[NSNumber" here.
|
||
**For example:** | ||
```objc | ||
typedef NS_ENUM(NSInteger, NYTDaysOfTheWeek) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIRC, for proper integration with Swift the name of the enum and the values should share the same prefix. Such as:
typedef NS_ENUM(NSInteger, NYTWeekday) {
NYTWeekdayMonday = 0,
NYTWeekdayTuesday
};
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. ❤️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mattbischoff not familiar with the Swift integration issue you're bringing up, could you point me in a direction regarding that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking about this more, I’m not sure it belongs in the style guide. It doesn’t really cover any new ground stylistically. What do you all think? |
I believe that by, "they cannot be directly used as boxed literals," @spacedrabbit is referencing Clang's (vaguely stated) decision to enforce Considering boxed literals— |
What’s the status of this request? |
I think this suggestion is subsumed by Literals and enforced by Clang. I would not merge this PR. Anybody else? |
Makes sense to me. |
Creating PR for issue: #93 to explain the proposal. Let me know if this is worth explicitly mentioning/adding in or if this needs syntax changes.
closes #93