5、用枚举值表示状态、选项、状态码

一、C语言中
1、C语言中的枚举类型enum。在以一系列常量来表示错误状态码或可组合的选项时,极宜使用枚举为其命名。
2、定义一个enum
enum personAgeState {
personAgeStateLitter,
personAgeStateBigger
};
@property (nonatomic, assign) enum personAgeState personAge;
enum personAgeState personAge = personAgeStateLitter;
3、可以使用typedef关键字重新定义枚举类型
enum personAgeState {
personAgeStateLitter,
personAgeStateBigger
};
typedef enum personAgeState personAgeState;
@property (nonatomic, assign) personAgeState personAge;
personAgeState personAge = personAgeStateLitter;
4、可以指明用何种“底层数据类型”来保存枚举类型的变量。这样做的好处是,可以向前声明枚举变量了。若不指定底层数据类型,则无法向前声明枚举类型,因为编译器不清楚底层数据类型的大小,所以在用到此枚举类型时,也就不知道究竟该给变量分配多少空间。
enum waitState : NSInteger {
waitStateLitter = 2,
waitStateStrong
};
@property (nonatomic, assign) enum waitState : NSInteger waitState;
enum waitState : NSInteger waitState1 = waitStateLitter;
5、在定义选项的时候,应该使用枚举类型。若这些选项可以彼此组合,则更应如此。只要枚举定义得对,各选项之间就可以通过 “按位或操作符”来组合。
enum UIViewAutoresizing {
UIViewAutoresizingNone                 = 0,
UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,
UIViewAutoresizingFlexibleWidth        = 1 << 1,
UIViewAutoresizingFlexibleRightMargin  = 1 << 2,
UIViewAutoresizingFlexibleTopMargin    = 1 << 3,
UIViewAutoresizingFlexibleHeight       = 1 << 4,
UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};
如果把传递给某个方法的选项表示为枚举类型,而多个选项又可以同时使用,那么就将各选项值定义为2的幂,以便通过按位或操作将其组合起来。
二、OC语言中
Foundation框架中定义了一些辅助的宏,用这些宏来定义枚举类型时,也可以制定用于保存枚举值的底层数据类型。这些宏具备向后兼容能力,如果目标平台的编译器支持新标准,那就使用新式语法,否则改用旧式语法。
1、
typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {
UIViewAnimationTransitionNone,
UIViewAnimationTransitionFlipFromLeft,
UIViewAnimationTransitionFlipFromRight,
UIViewAnimationTransitionCurlUp,
UIViewAnimationTransitionCurlDown,
};
上面用NS_ENUM宏所定义的枚举类型展开之后就是:
typedef enum UIViewAnimationTransition : NSInteger UIViewAnimationTransition;
enum UIViewAnimationTransition : NSInteger {
UIViewAnimationTransitionNone,
UIViewAnimationTransitionFlipFromLeft,
UIViewAnimationTransitionFlipFromRight,
UIViewAnimationTransitionCurlUp,
UIViewAnimationTransitionCurlDown,
};
2、
typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {
UIViewAutoresizingNone                 = 0,
UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,
UIViewAutoresizingFlexibleWidth        = 1 << 1,
UIViewAutoresizingFlexibleRightMargin  = 1 << 2,
UIViewAutoresizingFlexibleTopMargin    = 1 << 3,
UIViewAutoresizingFlexibleHeight       = 1 << 4,
UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};
三、枚举值与switch语句
在处理枚举类型的switch语句中不要实现default分支。这样的话,假如新枚举之后,编译器就会提示开发者:switch语句并未处理所有枚举。

转载于:https://www.cnblogs.com/cchHers/p/6946579.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/416600.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

openjdk17体验

jdk17是继jdk8和jdk11之后的LTS版本&#xff0c;该版本支持到2029年12月&#xff0c;下载地址 https://jdk.java.net/17/ 我是ubuntu64位环境&#xff0c;下载linux/x64版本&#xff0c;解压 打开idea创建一个新项目&#xff0c;添加jdk17并设置为jdk17 写个helloworld packa…

linux zip分卷压缩解压命令

linux下有时因为文件过大&#xff0c;传输过程中需要将源文件压缩为多个zip文件&#xff0c;以下是具体方法 压缩 zip -r -s 1g split.zip fold/ -s 1g代表分卷大小 split.zip为压缩包名 fold/为待压缩的目录 解压 zip -s 0 split.zip --out unsplit.zip unzip unslit.zip…

不确定屏幕大小的弹窗垂直居中(用了box方法)

对于父元素高度不确定&#xff0c;又要实现上下左右对齐&#xff0c;只需要在弹窗的父元素上使用box布局&#xff0c;控制弹窗上下左右居中 <div class"pop_wrap"><div class"pop"></div> </div> .pop_wrap{display:-webkit-box;-…

npm i依赖版本兼容问题处理

npm i报错提示 whqwhq-Z270P-D3:/data/code/builder_git/xx$ npm i npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! While resolving: webbq-cmm1.1.0 npm ERR! Found: webpack3.12.0 npm ERR! node_modules/webpack npm ER…

前端学习(2449):发布文章组件

request.js <template> <div class"artical-container"><!--卡片--><el-card class"filter-card"><div slot"header" class"clearfix"><!--面包屑导航--><el-breadcrumb separator-class&quo…

centos7 geenplum5.x postgis开源版本编译

下载5.1 https://codeload.github.com/greenplum-db/gpdb/tar.gz/5.1.0 基础环境 tar xzvf gpdb-5.1.0.tar.gz cd gpdb-5.1.0 参照README.md、README.amazon_linux准备依赖 yum install -y epel-release yum install -y python-pip sudo yum -y install git gcc readline-dev…

linux查看基础硬件信息

#CPU核数 cat /proc/cpuinfo | grep "cpu cores" | uniq #CPU线程数 cat /proc/cpuinfo| grep "processor"| wc -l #内存大小 cat /proc/meminfo | grep MemTotal #硬盘信息 fdisk -l

前端学习(2450):页面布局制作

request.js <template> <div class"artical-container"><!--卡片--><el-card class"filter-card"><div slot"header" class"clearfix"><!--面包屑导航--><el-breadcrumb separator-class&quo…

前端学习(2451):表单数据的绑定

request.js <template> <div class"artical-container"><!--卡片--><el-card class"filter-card"><div slot"header" class"clearfix"><!--面包屑导航--><el-breadcrumb separator-class&quo…

mysql string types ---- mysql 字符类型详解

一、mysql 中包涵的字符类型&#xff1a; [national] char [(m)] [character set charset_name] [collate collation_name] [national] varchar [(m)] [character set charset_name] [collate collation_name]binary(m) -- 和char 只不过它用来保存二进制字节串…

前端学习(2452):封装数据接口

request.js <template> <div class"artical-container"><!--卡片--><el-card class"filter-card"><div slot"header" class"clearfix"><!--面包屑导航--><el-breadcrumb separator-class&quo…

【NOIP2016】换教室

题目描述 对于刚上大学的牛牛来说, 他面临的第一个问题是如何根据实际情况中情合适的课程。 在可以选择的课程中,有2n节课程安排在n个时间段上。在第 i ( 1≤ i≤n)个时同段上, 两节内容相同的课程同时在不同的地点进行, 其中, 牛牛预先被安排在教室 ci上课, 而另一节课程在教室…

react和react develop tools编译

下载 https://github.com/facebook/react/tags 我这里下载的18.0.0 下载react&#xff0c;里面的packages/react-devtools-extensions目录就是chrome等的扩展插件&#xff0c;需要编译下 需要yarn环境编译&#xff0c;如果需要安装yarn&#xff0c;使用命令 npm install --…

前端学习(2453):项目初始化

# 一、项目初始化## 使用 Vue CLI 创建项目> 注意&#xff1a;不要使用 Git Bash 执行项目创建操作&#xff0c;使用 cmd 或者 powershell 之类的工具。> 如果你还没有安装 VueCLI&#xff0c;或者版本低于 4&#xff0c;请执行下面的命令安装或是升级&#xff1a; >…

python爬虫:两种方法模拟登录博客园

第一方法用第三方库&#xff08;requests&#xff09;&#xff1a;参考http://www.mamicode.com/info-detail-1839685.html 源代码分析 博客园的登录页面非常简单&#xff0c;查看网页源代码&#xff0c;可以发现两个输入框的id分别为input1、input2&#xff0c;复选框的id为re…

java zip压缩解压代码,亲测可用,压缩文件不会有合并问题

亲测可用&#xff0c;压缩文件不会有合并问题 import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStrea…

前端学习(2454):用户登录

# 二、用户登录## 功能介绍测试账号&#xff1a;- 13911111111 - 246810也可以通过我们这个应用的移动端注册一个自己的账号&#xff1a;地址&#xff1a;http://vue-toutiao-m.lipengzhou.com/#/login- 手机号&#xff1a;你自己的- 验证码&#xff1a;- 246810- 也可以动态接…

OSError: [Errno 1] Operation not permitted 问题解决

如果在mac下碰到OSError: [Errno 1] Operation not permitted:的问题&#xff0c;就算用sudo 也无法解决. 例如&#xff1a; pip install ipython --user -U 转载于:https://www.cnblogs.com/nemolmt/p/6991408.html