Rumah  >  Soal Jawab  >  teks badan

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

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

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

漂亮男人漂亮男人2705 hari yang lalu688

membalas semua(1)saya akan balas

  • 阿神

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

    UIRectCornerTopLeft dan UIRectCornerTopRight yang ditunjukkan dalam kod anda sebenarnya bukan penghitungan, tetapi 按位掩码(bitmask), dan takrifannya adalah seperti berikut:

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

    Sintaks topeng bitwise (NS_OPTIONS) adalah sama dengan penghitungan (NS_ENUM), tetapi pengkompil akan menggabungkan nilainya melalui bitmask |.

    Editor:

    Sebagai contoh, untuk NS_OPTIONS UIRectCorner di atas, mengikut kod anda, anda masukkan UIRectCornerTopLeft | UIRectCornerTopRight , maka kod pemprosesan adalah kira-kira seperti berikut:

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

    balas
    0
  • Batalbalas