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

swift学习笔记之枚举

发布时间:2020-12-14 06:56:57 所属栏目:百科 来源:网络整理
导读://枚举类型 ==成员+计算属性,类型属性+下标脚本+方法 //enum 的关键字不能省略 //1定义任意类型的枚举成员 enum anyEnum{ case North(Int) case South(String) case East(Double) case West(String)} //North 可以赋值任意类型 //2指定类型的枚举成员 enum D
//枚举类型 ==成员+计算属性,类型属性+下标脚本+方法
//enum 的关键字不能省略
//1>定义任意类型的枚举成员

enum anyEnum{
    case North(Int)
    case South(String)
    case East(Double)
    case West(String)
}
//North 可以赋值任意类型

//2>指定类型的枚举成员
enum Derication {
    case North
    case South(Int)
    case East
    case West
}
//North 只能字符串
//3.可以初始化枚举成员的值,但是原始值必须是唯一的,不能相同
//4必须定义成员,不能一个成员也没有

//注意1case不能省略 2 可以一个case 多个成员,写成一行
enum Derication1:String {
    case North,South,East,West
}

//5实例化枚举类型的对象
// let/var 枚举对象=枚举类型.成员名

let newVlaue=Derication1.North

//6.访问枚举类型中成员的原始值

  //1>let str=Derication1.North.rawValue
  //2>自动自增,如果North不设置值 为 0,1,2

enum Derication2:Int {
    case North=3
    case South//4
    case East//5
    case West//6
}
  //3 通过原始值获取成员
enum Zoo:Int {

    case Dog=5
    case Cat=10
    case Pig=20
}

let ace=Zoo.Dog
let dogRawValue=ace.rawValue
print(dogRawValue)

//判断 获取成员所对应的值(待查找问题)
//if let convertedZoo = Zoo.fromValue {
//
//}

//7枚举成员与swithch匹配

switch ace {

case .Dog:
    print("Dog")
case .Cat:
    print("Cat")
default:  //default不能省
    print("Pig")

}

//8成员关联值

let mySouth = Derication.South(100)
//注意 @1.个数@数据类型 @3指定数据类型的枚举类型不需要关联值

//9定义计算属性,类型属性,属性监视器 枚举对象名.属性
//10 可以定义下标脚本 成员[index]
//枚举里可以定义方法 @1 成员.方法名
// @2 类型方法 方法前加staic 枚举类型名.方法名(参数)



//类.枚举类型,结构的区别
//1>类是引用类型,支持继承
//2结构和枚举类型 是值类型,不支持继承,
//3三种可以相互嵌套

(编辑:李大同)

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

    推荐文章
      热点阅读