IOS枚举学习

ゞ 浴缸里的玫瑰 2022-08-06 11:21 271阅读 0赞
  1. 枚举就是自己定义的一些常量,而这些常量是根据自己的需要定义不同的用途。 有两种形式定义枚举。代码如下:
  2. #import <Foundation/Foundation.h>
  3. //枚举在ios中一般使用前缀+枚举类型+属性/状态 这样,首字母和单词首字母大写,例如UI+ControlState+Normal
  4. //typedef enum
  5. //{
  6. // ColorBlack = 0,
  7. // ColorGreen = 10,
  8. // ColorBlue = 20,
  9. // ColorRed // 没有标明的话,就是递增加 1
  10. //
  11. //} Color;
  12. enum Color
  13. {
  14. ColorBlack = 0,
  15. ColorGreen = 10,
  16. ColorBlue = 20,
  17. ColorRed // 没有标明的话,就是递增加 1
  18. } ;
  19. @interface EnumStu : NSObject
  20. //@property(nonatomic) Color color;
  21. @property(nonatomic) enum Color color;
  22. -(void) showEnumMsg;
  23. //-( Color) returnColor;
  24. -(enum Color) returnColor;
  25. @end
  26. #import "EnumStu.h"
  27. @implementation EnumStu
  28. @synthesize color;
  29. -(void)showEnumMsg
  30. {
  31. NSLog(@"enum--->%u",self.color);
  32. }
  33. //-( Color)returnColor{}
  34. -(enum Color)returnColor
  35. {
  36. NSLog(@"返回enum--->%u",self.color);
  37. 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>
  38. </div>
  39. }
  40. return self.color;
  41. }
  42. @end

调用

  1. EnumStu *enumStu = [[EnumStu alloc] init];
  2. enumStu.color = ColorGreen;
  3. [enumStu showEnumMsg];
  4. [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

  1. 简单调用和定义。

发表评论

表情:
评论列表 (有 0 条评论,271人围观)

还没有评论,来说两句吧...

相关阅读

    相关 iOS用法

    ![enum.png][] 枚举是C语言中的一种基本数据类型,是一个”被命名的整型常量”的集合,它不参与内存的占用和释放,我们在开发中使用枚举的目的只有一个,那就是为了增加代

    相关 IOS学习

               枚举就是自己定义的一些常量,而这些常量是根据自己的需要定义不同的用途。 有两种形式定义枚举。代码如下:          import <

    相关 学习笔记

    [2019独角兽企业重金招聘Python工程师标准>>> ][2019_Python_] ![hot3.png][] 枚举Enum > 关键字enum可以将一组具名的值