加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

【swift3.0】【枚举定义的不同方式】

发布时间:2020-12-14 06:33:21 所属栏目:百科 来源:网络整理
导读:贡献作者 -【XJDomain】 博客XJ: https://my.oschina.net/shengbingli/blog GitHub直播地址 : https://github.com/lishengbing/XJDomainLive XJ--01 定义字符串类型的枚举 enum kBackgroundImageNameType : String { // 蓝色默认 case navBarBg_04BEC6 = "na

贡献作者 -【XJDomain】
博客XJ: https://my.oschina.net/shengbingli/blog
GitHub直播地址: https://github.com/lishengbing/XJDomainLive

XJ--01

> 定义字符串类型的枚举

enum kBackgroundImageNameType : String {
    // 蓝色<默认>
    case navBarBg_04BEC6 = "navBarBg_04BEC6"
    // 橘色
    case navBarBg_FFAB6D = "navBarBg_FFAB6D"
    // 灰色
    case navBarBg_E7E7E7 = "navBarBg_E7E7E7"
}

使用一:

kBackgroundImageNameType.navBarBg_04BEC6.rawValue

XJ--02

> 定义没有类型的枚举:

enum MethodType {
    case get
    case post
}

使用二:

let method = type == .get ? HTTPMethod.get : HTTPMethod.post

XJ--03

> 枚举的完美解析:

// 1:枚举类型的定义
enum MethodType {
    case get
    case post
    case put
    case delete
}

enum MethodType1 : String {
    case get = "get"
    case post = "post"
    case put = "put"
    case delete = "delete"
}

// 2:创建枚举具体的值
let type1 : MethodType = .get
let type2 = MethodType.post

// 3:给枚举类型进行赋值
//  如果枚举没有赋值的话是没有值的,不像oc中默认依次是0.1.2.3....
enum Direction : Int {
    case east = 0
    case west = 1
    case north = 2
    case south = 3
}

let d : Direction = .east
// 只有赋值才会有这种创建方式:可选类型
let d1 = Direction(rawValue: 1)


// 4:枚举类型定义方式二
enum Type  {
    case get,post,put,delete
}
// 只针对Int类型,会自动为0.1.2.3
enum Type1 : Int  {
    case get = 0,delete
}

let type111 = Type1(rawValue: 1)
print(type111)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读