Maison  >  Questions et réponses  >  le corps du texte

objective-c - ios , 关于多 参数 枚举 的实现?

我想实现 可以 传入 多参数枚举值的方法,例如

,请教一下,方法里面的逻辑判断

漂亮男人漂亮男人2705 Il y a quelques jours690

répondre à tous(1)je répondrai

  • 阿神

    阿神2017-05-02 09:21:11

    Les UIRectCornerTopLeft et UIRectCornerTopRight affichés dans votre code ne sont en fait pas des énumérations, mais 按位掩码(bitmask), et leur définition est la suivante :

    typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
        UIRectCornerTopLeft     = 1 << 0,
        UIRectCornerTopRight    = 1 << 1,
        UIRectCornerBottomLeft  = 1 << 2,
        UIRectCornerBottomRight = 1 << 3,
        UIRectCornerAllCorners  = ~0UL
    };

    La syntaxe d'un masque au niveau du bit (NS_OPTIONS) est la même qu'une énumération (NS_ENUM), mais le compilateur combinera ses valeurs via un masque de bits |.

    Editeur :

    Par exemple, pour les NS_OPTIONS de UIRectCorner ci-dessus, selon votre code, vous passez UIRectCornerTopLeft | UIRectCornerTopRight , alors le code de traitement est à peu près le suivant :

    UIRectCorner myRectCornerOptions = UIRectCornerTopLeft | UIRectCornerTopRight; // 你在方法里接收到值应该是这个。
    
    // 对传入的 NS_OPTIONS 的处理逻辑:
    if (myRectCornerOptions & UIRectCornerTopLeft) {
        // 包含了 UIRectCornerTopLeft。
    } else {
        // 未包含 UIRectCornerTopLeft。
    }
        
    if (myRectCornerOptions & UIRectCornerTopRight) {
        // 包含了 UIRectCornerTopRight。
    } else {
        // 未包含 UIRectCornerTopRight。
    }

    répondre
    0
  • Annulerrépondre