gpt4 book ai didi

enums - 如何将 typedef 枚举转换为 NSNumber?

转载 作者:行者123 更新时间:2023-12-02 02:12:05 24 4
gpt4 key购买 nike

  1. 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:] 是正确的使用方法,因为 accepted answer to Best way to implement Enums with Core Data使用它。例如:

    [NSNumber numberWithInt:UIViewAnimationCurveLinear]

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

  2. 对于按位枚举,例如:

    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]

最佳答案

现在可以使用现代语法:

@(UIViewAnimationCurveLinear)

关于enums - 如何将 typedef 枚举转换为 NSNumber?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10255145/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com