Swift 六:枚举

1).枚举的定义及使用

 定义语法:

枚举类型  枚举名 : 枚举值类型 {

     case 类型名1   

     case 类型名2

     ...

}   //枚举值类型可省略 默认Int

如:

enum Method {

      case add

      case sub

      case Mul

      case Dlv

}

调用:(可结合Swift的类型推断机制

let case1 = Method.add

let case2 : Method = .sub


结合switch case使用枚举:

func chooseMethod(method : Method) {

     switch method {

        case .add: break

         default : break

    }

}


2).枚举关联值和值绑定

enum Method {

    case add (start : Double , end : Double)

    case sub (start : Double , end : Double)

}

let case1 = Method.add(start: 11, end: 10)

func chooseMethod(method : Method) {

    switch method {

        case let .add(start: e, end: _) :

             print(e)

       default : break

   }

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容