星空网 > 软件开发 > 操作系统

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

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


2、定义枚举类型“FamilyRelationshipType”

enum FamilyRelationshipType {  case GrandFather  case GrandMother  case Father  case Mother  case Brother  case Sister  case Son  case Daughter}

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

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

4、指定值定义枚举:

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中使用枚举实例:

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 中把每个枚举与它的值联合起来。

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)}

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

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  }}

至此完整的Swift下的枚举定义使用完成,顺便推荐一下个人ios开发小站http://www.iosask.com




原标题:Swift优雅的实现枚举的定义及使用

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

小家电又出海逆袭 !这家大卖一年创下破亿营收:https://www.kjdsnews.com/a/1724938.html
400+店铺被冻结!名单已公布,卖家抓紧自查下架!:https://www.kjdsnews.com/a/1725926.html
2024春节流量大战 :https://www.kjdsnews.com/a/1726926.html
APP海外KOL营销如何衡量投放效果?这5个指标你知道吗?:https://www.kjdsnews.com/a/1726927.html
美国又出新规!一批卖家将受影响:https://www.kjdsnews.com/a/1726928.html
如果这些省钱的运营技巧你都没有用上,活该你利润低:https://www.kjdsnews.com/a/1726929.html
十月北方旅游最佳去处?:https://www.vstour.cn/a/365184.html
缅甸电子签证口岸 缅甸电子签证在线申请:https://www.vstour.cn/a/365185.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流