Swift优雅的实现枚举的定义及使用详解

1、枚举是用来表示值的“类型”。


2、定义枚举类型“FamilyRelationshipType”

1
2
3
4
5
6
7
8
9
10
enum  FamilyRelationshipType {
     case  GrandFather
     case  GrandMother
     case  Father
     case  Mother
     case  Brother
     case  Sister
     case  Son
     case  Daughter
}


3、使用枚举,指定该成员与其他成员的关系:

1
2
3
4
5
6
7
8
9
10
11
struct  FamilyMember {
     var name: String
     var relationshipType: FamilyRelationshipType
    
     init(name: String, relationshipType: FamilyRelationshipType) {
         self.name = name
         self.relationshipType = relationshipType
     }
}
let sister = FamilyMember(name:  "Jacky" , relationshipType: FamilyRelationshipType.Sister)
let brother = FamilyMember(name:  "Jack" , relationshipType: FamilyRelationshipType.Brother)


4、指定值定义枚举:

1
2
3
4
5
6
7
8
9
10
enum  FamilyRelationshipType: Int {
     case  GrandFather = 400
     case  GrandMother = 500
     case  Father = 600
     case  Mother = 700
     case  Brother = 800
     case  Sister = 900
     case  Son = 1000
     case  Daugther = 1100
}


5、Switch中使用枚举实例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
static  func giftFor(member: FamilyMember) -> String {
     switch  (member.relationshipType) {
     case  .GrandFather:
         return  "Book"
     case  .GrandMother:
         return  "Sweater"
     case  .Father:
         return  "Shirt"
     case  .Mother:
         return  "Flowers"
     default :
         return  "Choclates"
     }
}


6、Swift 中也可以把每个枚举与它的值联合起来。

1
2
3
4
5
6
7
8
9
10
11
12
enum  FamilyRelationshipType {
     case  GrandFather(age: Int)
     case  GrandMother(age: Int)
     case  Father(age: Int)
     case  Mother(age: Int)
     case  Husband(age: Int)
     case  Wife(age: Int)
     case  Brother(age: Int)
     case  Sister(age: Int)
     case  Son(age: Int)
     case  Daugther(age: Int)
}

这些联合值(associated values)只可以在Switch语句里使用枚举时访问。


完整实例,根据家庭成员的年龄大小来决定具体的礼物:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
enum  FamilyRelationshipType {
     case  GrandFather(age: Int)
     case  GrandMother(age: Int)
     case  Father(age: Int)
     case  Mother(age: Int)
     case  Husband(age: Int)
     case  Wife(age: Int)
     case  Brother(age: Int)
     case  Sister(age: Int)
     case  Son(age: Int)
     case  Daugther(age: Int)
    
     func gift() -> String {
         switch (self) {
         case  .Brother(let age):
             if  age > 10 {
                 return  "video games"
             else  {
                 return  "toys"
             }
         case  .GrandFather:
             return  "Book"
         case  .GrandMother:
             return  "Sweater"
         case  .Father:
             return  "Shirt"
         case  .Mother:
             return  "Flowers"
         default :
             return  "Choclates"
         }
     }
}
struct  FamilyMember {
     var name: String
     var relationshipType: FamilyRelationshipType
    
     init(name: String, relationshipType: FamilyRelationshipType) {
         self.name = name
         self.relationshipType = relationshipType
     }
}

 

更多技术讨论:http://www.iosask.com

转载于:https://my.oschina.net/u/588534/blog/510552

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值