14
  1. a 的每个值是否都typedef enum被视为int

    例如,给定以下内容typedef enum

    // UIView.h
    
    typedef enum {
        UIViewAnimationCurveEaseInOut,
        UIViewAnimationCurveEaseIn,
        UIViewAnimationCurveEaseOut,
        UIViewAnimationCurveLinear
    } UIViewAnimationCurve;
    

    我如何知道使用哪种方法来创建NSNumber?

    + (NSNumber *)numberWithShort:(short)value;
    + (NSNumber *)numberWithUnsignedShort:(unsigned short)value;
    + (NSNumber *)numberWithInt:(int)value;
    + (NSNumber *)numberWithUnsignedInt:(unsigned int)value;
    + (NSNumber *)numberWithLong:(long)value;
    + (NSNumber *)numberWithUnsignedLong:(unsigned long)value;
    + (NSNumber *)numberWithLongLong:(long long)value;
    + (NSNumber *)numberWithUnsignedLongLong:(unsigned long long)value;
    + (NSNumber *)numberWithInteger:(NSInteger)value NS_AVAILABLE(10_5, 2_0);
    + (NSNumber *)numberWithUnsignedInteger:(NSUInteger)value NS_AVAILABLE(10_5, 2_0);
    

    我认为这+[NSNumber numberWithInt:]是正确的使用方法,因为使用 Core Data 实现枚举的最佳方法的公认答案使用了它。例如:

    [NSNumber numberWithInt:UIViewAnimationCurveLinear]
    

    但是,如果+[NSNumber numberWithInt:]是正确的,那为什么呢?

  2. 对于按位enum,例如:

    enum {
        UIViewAutoresizingNone                 = 0,
        UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,
        UIViewAutoresizingFlexibleWidth        = 1 << 1,
        UIViewAutoresizingFlexibleRightMargin  = 1 << 2,
        UIViewAutoresizingFlexibleTopMargin    = 1 << 3,
        UIViewAutoresizingFlexibleHeight       = 1 << 4,
        UIViewAutoresizingFlexibleBottomMargin = 1 << 5
    };
    typedef NSUInteger UIViewAutoresizing;
    

    我猜这+[NSNumber numberWithUnsignedInteger:]是正确的使用方法,因为有一个明确的NSUIntegerafter typedef。正确的?例如:

    [NSNumber numberWithUnsignedInteger:UIViewAutoresizingNone]
    
4

2 回答 2

23

现在可以使用现代语法:

@(UIViewAnimationCurveLinear)

于 2015-04-04T13:39:03.983 回答
7

[NSNumber numberWithInt:your_enum_int_variable];

于 2012-12-13T00:31:01.920 回答