NS_ENUM和NS_OPTIONS

OC由于是C的超集,所以可以直接用enum来声明枚举。

但如果想要使用NSInteger作为enum的底层类型。

需要两步:声明枚举enum,再typedef。

使用NS_ENUM直接一步搞定。

//
//  main.m
//  Hello Objective-C
//
//  Created by admin on 2020/11/16.
//

@import Foundation;

enum Lesson {
  math,art,music
};

typedef NS_ENUM(NSInteger, Work) {
    engineer,
    teacher,
    doctor,
    others,
};

typedef NS_OPTIONS(NSInteger, Weather) {
    sunny = 0,
    rainy = 1 << 0,
    snowy = 1 << 1,
    foxy = 1 << 2,
};

int main(int argc, char* argv[]) {
    Weather x = rainy | snowy;  // 雨夹雪
    if (x & sunny) {
        printf("晴天\n");
    }
    if (x & foxy) {
        printf("下雾了\n");
    }
    if (x & snowy) {
        printf("下雪了\n");
    }
    if (x & rainy) {
        printf("下雨了\n");
    }
    return 0;
}

 

posted @ 2020-12-20 19:50  NeoZy  阅读(175)  评论(0编辑  收藏  举报