iOS中关于NS_ENUM和NS_OPTIONS之间的差别

iOS枚举代码:

1、NS_ENUM:放的是整形,代码:

typedef NS_ENUM(NSInteger, MyCountry) {
    MyCountryDefault,
    MyCountryCustom
};

2:NS_OPTIONS:放的是位掩码,代码:

typedef NS_OPTIONS(NSInteger, MyCountry){
    MyCountry1 = 1 << 0,
    MyCountry2 = 1 << 1,
};

区别主要还是看适合用在什么地方:

枚举数据代表的是2个国家,第一个值zhi是缺省国家,第二值个是自定义国家:

如果只是简单判断一下国家的话可以这样用:

switch (style){
    case MyCountryDefault:
        // int is 0
    break;
    case MyCountryCustom:
        // int is 1
    break;
}

但是,如果想要判断多个国家的话,这样用就有点勉强了,但是可以使用位掩码是完成该功能:

typedef NS_OPTIONS(NSInteger, MyCountry) {
    MyCountry1 = 1 << 0, // bits: 0001
    MyCountry2 = 1 << 1, // bits: 0010
};

if (option & MyCountry1){ // last bit is 1
    // bits are 0001 or 0011
}
if (option & MyCountry2){ // second to last bit is 1
    // bits are 0010 or 0011
}
if (option & MyCountry1 & MyCountry2){ // last two bits are 1
    // bits are 0011
}

具体用哪种枚举还是要看具体情况。

转载于:https://my.oschina.net/caijunrong/blog/488427

uvm_active_passive_enum 是 UVM (Universal Verification Methodology) 定义的一个枚举类型,用于区分 UVM 组件(如 agent)的工作模式是主动(active)还是被动(passive)。在 UVM ,agent 组件可以是主动的,也可以是被动的,这取决于其子组件的行为。 - 主动模式(active): 在主动模式下,agent 会生成事务(transactions),并将其发送到被测设备(DUT)。主动 agent 通常包含 sequencer,它负责从 sequencer 获取序列并发送事务到驱动(driver)。 - 被动模式(passive): 被动模式下,agent 不生成事务,而是响应来自其他主动 agent 的事务。被动 agent 不包含 sequencer,而是可能包含监视器(monitor)和检查器(checker),用于观察通信并进行验证。 uvm_active_passive_enum 通常用于 agent 的构造函数,以便在创建 agent 实例时指定其模式。例如: ```verilog class my_agent extends uvm_agent; `uvm_component_utils(my_agent) uvm_active_passive_enum m_mode; // 枚举类型的成员变量 function new(string name, uvm_component parent); super.new(name, parent); endfunction virtual function void build_phase(uvm_phase phase); // 根据 m_mode 的值决定构建主动或被动 agent case(m_mode) UVM_ACTIVE: begin // 构建主动 agent 相关组件 end UVM_PASSIVE: begin // 构建被动 agent 相关组件 end default: `uvm_fatal("MODE", "Invalid agent mode") endcase endfunction endclass ``` 在 UVM 的配置,可以通过 uvm_config_db#() 来设置 agent 的模式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值