IOS枚举学习
枚举就是自己定义的一些常量,而这些常量是根据自己的需要定义不同的用途。 有两种形式定义枚举。代码如下:
#import <Foundation/Foundation.h>
//枚举在ios中一般使用前缀+枚举类型+属性/状态 这样,首字母和单词首字母大写,例如UI+ControlState+Normal
//typedef enum
//{
// ColorBlack = 0,
// ColorGreen = 10,
// ColorBlue = 20,
// ColorRed // 没有标明的话,就是递增加 1
//
//} Color;
enum Color
{
ColorBlack = 0,
ColorGreen = 10,
ColorBlue = 20,
ColorRed // 没有标明的话,就是递增加 1
} ;
@interface EnumStu : NSObject
//@property(nonatomic) Color color;
@property(nonatomic) enum Color color;
-(void) showEnumMsg;
//-( Color) returnColor;
-(enum Color) returnColor;
@end
#import "EnumStu.h"
@implementation EnumStu
@synthesize color;
-(void)showEnumMsg
{
NSLog(@"enum--->%u",self.color);
}
//-( Color)returnColor{}
-(enum Color)returnColor
{
NSLog(@"返回enum--->%u",self.color);
switch (self.color) { <p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; color: rgb(49, 89, 93);"><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2"> case</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span>ColorBlack<span style="font-variant-ligatures: no-common-ligatures; color: #000000">:</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">NSLog</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">@"---%@"</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">@"</span><span style="font-family: 'Heiti SC Light'; color: rgb(209, 47, 27);">选择了黑色</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">"</span>);</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">break</span>;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">case</span> <span style="font-variant-ligatures: no-common-ligatures; color: #31595d">ColorGreen</span>:</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">NSLog</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">@"---%@"</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">@"</span><span style="font-family: 'Heiti SC Light'; color: rgb(209, 47, 27);">选择了绿色</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">"</span>);</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;"> </p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">break</span>;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">case</span> <span style="font-variant-ligatures: no-common-ligatures; color: #31595d">ColorBlue</span>:</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">NSLog</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">@"---%@"</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">@"</span><span style="font-family: 'Heiti SC Light'; color: rgb(209, 47, 27);">选择了蓝色</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">"</span>);</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;"> </p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">break</span>;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">case</span> <span style="font-variant-ligatures: no-common-ligatures; color: #31595d">ColorRed</span>:</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">NSLog</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">@"---%@"</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">@"</span><span style="font-family: 'Heiti SC Light'; color: rgb(209, 47, 27);">选择了红色</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b">"</span>);</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo; min-height: 13px;"> </p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">break</span>;</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">default</span>:</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; font-family: Menlo;"> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">break</span>;</p><div>
</div>
}
return self.color;
}
@end
调用
EnumStu *enumStu = [[EnumStu alloc] init];
enumStu.color = ColorGreen;
[enumStu showEnumMsg];
[enumStu returnColor];
2015-01-19 23:07:26.557 OC**基础学习**[1025:57363] enum—->10
2015-01-19 23:07:26.558 OC**基础学习[1025:57363]返回**enum—->10
2015-01-19 23:07:26.558 OC**基础学习[1025:57363] —-选择了绿色**
Program ended with exit code: 0
简单调用和定义。
还没有评论,来说两句吧...