【问题标题】:Using Multiple NSUInteger enums as a parameter to a method使用多个 NSUInteger 枚举作为方法的参数
【发布时间】:2010-08-02 22:58:20
【问题描述】:

我正在尝试创建一个与 NSView 的 setAutoresizingMask: 方法具有相似格式的方法。我希望有人能够指定我在枚举(NSHeightSizable | NSWidthSizable)中声明的多个值,就像在自动调整掩码中一样。我该怎么做?

【问题讨论】:

    标签: objective-c cocoa enums nsuinteger


    【解决方案1】:

    首先,在标题中声明您的标志:

    enum
    {
        AZApple = (1 << 0),
        AZBanana = (1 << 1),
        AZClementine = (1 << 2),
        AZDurian = (1 << 3)
    };
    
    typedef NSUInteger AZFruitFlags;
    

    (1 &lt;&lt; 0)(1 &lt;&lt; 3) 表示整数中的单个位,您可以在整数中“屏蔽”进出。例如,假设NSUInteger 是 32 位的,并且有人同时选择了苹果和榴莲,那么整数将如下所示:

    0000 0000 0000 0000 0000 0000 0000 1001
                                       |  |- Apple bit
                                       |---- Durian bit
    

    通常,您的方法需要采用无符号整数参数:

    - (void) doSomethingWithFlags:(AZFruitFlags) flags
    {
        if (flags & AZApple)
        {
            // do something with apple
    
            if (flags & AZClementine)
            {
                // this part only done if Apple AND Clementine chosen
            }
        }
    
        if ((flags & AZBanana) || (flags & AZDurian))
        {
            // do something if either Banana or Durian was provided
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多