2012-04-21 101 views
11
  1. 是否將typedef enum的每個值視爲int如何將typedef枚舉轉換爲NSNumber?

    例如,給出以下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:]是使用的,因爲accepted answer to Best way to implement Enums with 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:]是使用,因爲有typedef後明確NSUInteger正確的方法。正確?例如爲:

    [NSNumber numberWithUnsignedInteger:UIViewAutoresizingNone] 
    

回答

16

如今現代語法可以用於:

@(UIViewAnimationCurveLinear)

6

[NSNumber的numberWithInt:your_enum_int_variable];

+0

你搖滾! 謝謝你。 – 2015-11-06 07:10:57