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

     

        简单调用和定义。  


      


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值