ios 枚举类型学习笔记
2015-09-16 09:37
363 查看
枚举类型定义
enum 枚举标示符
{
数据表;
}
例子:
enum style
{
style1,
style2
}
这里默认a=0;b=2.如果你把这个例子定义成一下这样:
enum style2
{
style21 = 2,
style22
}
那么这里a=2。而b就会等于3。这里需要注意的是,枚举style1的值等于1。可以把它直接赋值给int类型的变量。例如。
int a = style21;这是可以的。这个时候a=2.当然nsinteger也是可以的。
typedef 和enum一起合用的用法。
typedef enum则是用来定义一个数据类型,那么该类型的变量值只能在enum定义的范围内取。
typedef enum {
UIButtonTypeCustom = 0, //
no button type
UIButtonTypeRoundedRect, // rounded rect, flat white button, like in address card
UIButtonTypeDetailDisclosure,
UIButtonTypeInfoLight,
UIButtonTypeInfoDark,
UIButtonTypeContactAdd,
} UIButtonType;
UIButtonType表示一个类别,它的值只能表示大括号里面的数据表里面的每一个值,也就是UIButtonTypeCustom,UIButtonTypeRoundedRect,。。。。
以下都是贴的:
在iOS6和Mac OS 10.8以后Apple引入了两个宏来重新定义这两个枚举类型,实际上是将enum定义和typedef合二为一,并且采用不同的宏来从代码角度来区分。
NS_OPTIONS一般用来定义位移相关操作的枚举值,我们可以参考UIKit.Framework的头文件,可以看到大量的枚举定义。
typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {
UIViewAnimationTransitionNone,//默认从0开始
UIViewAnimationTransitionFlipFromLeft,
UIViewAnimationTransitionFlipFromRight,
UIViewAnimationTransitionCurlUp,
UIViewAnimationTransitionCurlDown,
};
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
};
这两个宏的定义在Foundation.framework的NSObjCRuntime.h中:
#if (__cplusplus && __cplusplus >= 201103L && (__has_extension(cxx_strong_enums) || __has_feature(objc_fixed_enum))) || (!__cplusplus && __has_feature(objc_fixed_enum))
#define NS_ENUM(_type, _name) enum _name : _type _name; enum _name : _type
#if (__cplusplus)
#define NS_OPTIONS(_type, _name) _type _name; enum : _type
#else
#define NS_OPTIONS(_type, _name) enum _name : _type _name; enum _name : _type
#endif
#else
#define NS_ENUM(_type, _name) _type _name; enum
#define NS_OPTIONS(_type, _name) _type _name; enum
#endif
将
typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {
展开得到:
typedef enum UIViewAnimationTransition : NSInteger UIViewAnimationTransition;
enum UIViewAnimationTransition : NSInteger {
从枚举定义来看,NS_ENUM和NS_OPTIONS本质是一样的,仅仅从字面上来区分其用途。NS_ENUM是通用情况,NS_OPTIONS一般用来定义具有位移操作或特点的情况(bitmask)。
实际使用时,可以直接定义:
typedef enum : NSInteger {....} UIViewAnimationTransition;
等效于上述定义。
不知以上这位仁兄写的对不对。若是对。我只有一个理解,就我目前的知识储备,还有工作需要,就是NS_ENUM比[b]NS_OPTIONS用的更多,所以需要重点学习。[/b]
参考文档:
1. http://nshipster.com/ns_enum-ns_options/
2.http://iamthewalr.us/blog/2012/11/ns_enum-and-ns_options/
原文:http://blog.csdn.net/annkie/article/details/9877643
ns_neum ns_option学习文章
http://www.cnblogs.com/langtianya/p/3888924.html
enum 枚举标示符
{
数据表;
}
例子:
enum style
{
style1,
style2
}
这里默认a=0;b=2.如果你把这个例子定义成一下这样:
enum style2
{
style21 = 2,
style22
}
那么这里a=2。而b就会等于3。这里需要注意的是,枚举style1的值等于1。可以把它直接赋值给int类型的变量。例如。
int a = style21;这是可以的。这个时候a=2.当然nsinteger也是可以的。
typedef 和enum一起合用的用法。
typedef enum则是用来定义一个数据类型,那么该类型的变量值只能在enum定义的范围内取。
typedef enum {
UIButtonTypeCustom = 0, //
no button type
UIButtonTypeRoundedRect, // rounded rect, flat white button, like in address card
UIButtonTypeDetailDisclosure,
UIButtonTypeInfoLight,
UIButtonTypeInfoDark,
UIButtonTypeContactAdd,
} UIButtonType;
UIButtonType表示一个类别,它的值只能表示大括号里面的数据表里面的每一个值,也就是UIButtonTypeCustom,UIButtonTypeRoundedRect,。。。。
以下都是贴的:
在iOS6和Mac OS 10.8以后Apple引入了两个宏来重新定义这两个枚举类型,实际上是将enum定义和typedef合二为一,并且采用不同的宏来从代码角度来区分。
NS_OPTIONS一般用来定义位移相关操作的枚举值,我们可以参考UIKit.Framework的头文件,可以看到大量的枚举定义。
typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {
UIViewAnimationTransitionNone,//默认从0开始
UIViewAnimationTransitionFlipFromLeft,
UIViewAnimationTransitionFlipFromRight,
UIViewAnimationTransitionCurlUp,
UIViewAnimationTransitionCurlDown,
};
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
};
这两个宏的定义在Foundation.framework的NSObjCRuntime.h中:
#if (__cplusplus && __cplusplus >= 201103L && (__has_extension(cxx_strong_enums) || __has_feature(objc_fixed_enum))) || (!__cplusplus && __has_feature(objc_fixed_enum))
#define NS_ENUM(_type, _name) enum _name : _type _name; enum _name : _type
#if (__cplusplus)
#define NS_OPTIONS(_type, _name) _type _name; enum : _type
#else
#define NS_OPTIONS(_type, _name) enum _name : _type _name; enum _name : _type
#endif
#else
#define NS_ENUM(_type, _name) _type _name; enum
#define NS_OPTIONS(_type, _name) _type _name; enum
#endif
将
typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {
展开得到:
typedef enum UIViewAnimationTransition : NSInteger UIViewAnimationTransition;
enum UIViewAnimationTransition : NSInteger {
从枚举定义来看,NS_ENUM和NS_OPTIONS本质是一样的,仅仅从字面上来区分其用途。NS_ENUM是通用情况,NS_OPTIONS一般用来定义具有位移操作或特点的情况(bitmask)。
实际使用时,可以直接定义:
typedef enum : NSInteger {....} UIViewAnimationTransition;
等效于上述定义。
不知以上这位仁兄写的对不对。若是对。我只有一个理解,就我目前的知识储备,还有工作需要,就是NS_ENUM比[b]NS_OPTIONS用的更多,所以需要重点学习。[/b]
参考文档:
1. http://nshipster.com/ns_enum-ns_options/
2.http://iamthewalr.us/blog/2012/11/ns_enum-and-ns_options/
原文:http://blog.csdn.net/annkie/article/details/9877643
ns_neum ns_option学习文章
http://www.cnblogs.com/langtianya/p/3888924.html
相关文章推荐
- Android Native 绘图方法
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- C#中struct和class的区别详解
- 要慎用mysql的enum字段的原因
- VBS ArrayList Class vbs中的数组类
- 大家看了就明白了css样式中类class与标识id选择符的区别小结
- 深入了解PHP类Class的概念
- setAttribute 与 class冲突解决
- JavaScript中的类(Class)详细介绍
- javascript面向对象包装类Class封装类库剖析
- jQuery使用hide方法隐藏指定元素class样式用法实例
- jQuery给多个不同元素添加class样式的方法
- c++ 尽量不要使用#define 而是用const、enum、inline替换。
- mysql之set与enum的介绍