objective-c - ios , 关于多 参数 枚举 的实现?
漂亮男人
漂亮男人 2017-05-02 09:19:11
[iOS讨论组]

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

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

漂亮男人
漂亮男人

全部回复(1)
阿神

你代码里展示的 UIRectCornerTopLeft、UIRectCornerTopRight 其实并不是枚举,而是按位掩码(bitmask),它的定义如下所示:

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

按位掩码(NS_OPTIONS)的语法和枚举(NS_ENUM)相同,但编译器会将它的值通过位掩码 | 组合在一起。

编辑:

比如对于上面的 UIRectCorner 这个 NS_OPTIONS,按照你的代码,你传入的是 UIRectCornerTopLeft | UIRectCornerTopRight ,那么处理时候的代码大致如下:

UIRectCorner myRectCornerOptions = UIRectCornerTopLeft | UIRectCornerTopRight; // 你在方法里接收到值应该是这个。

// 对传入的 NS_OPTIONS 的处理逻辑:
if (myRectCornerOptions & UIRectCornerTopLeft) {
    // 包含了 UIRectCornerTopLeft。
} else {
    // 未包含 UIRectCornerTopLeft。
}
    
if (myRectCornerOptions & UIRectCornerTopRight) {
    // 包含了 UIRectCornerTopRight。
} else {
    // 未包含 UIRectCornerTopRight。
}
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号