iOS_Swift_枚举

需要注意的是Swift中枚举属于一级类型

import UIKit
/*
 swift枚举:
 (1)枚举基本语法
 Class One(一等类型)

 */
enum Method:String {
    //两种写法是相同的
    case Add = "add"
    case Sub = "sub"
    case Mul = "mul"
    case Div = "div"

//    case Add, Sub, Mul, Div
}
enum LineSegmentDescriptor{

    case StartAndEndPattern(start:Double , end:Double)
    case StartAndLenthPattern(start:Double , lenth:Double)

}

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        /*
         swift枚举:
         (1)枚举基本语法
         Class One(一等类型)
         (2)switch匹配枚举值
         (3)枚举原始值(),成员值
         (4)相关值
         */


//        (3)成员值
        print(chooseMethod(Method.Mul)(3,4))
        //!!!最新的Swift版本不再支持toRaw和fromRaw了,只有rawValue属性和hashValue属性了!
        print(Method.Add.hashValue)//哈希值
        print(Method.Add.rawValue)//原始值

//        (4)相关值
        var m0:Method = .Add
        var m1 = Method.Sub
        var lsd = LineSegmentDescriptor.StartAndEndPattern(start: 10, end: 20)
        switch lsd {
        case .StartAndEndPattern(let s , let e):
            print("\(s)   \(e)")
        case .StartAndLenthPattern(let s , let d):
            print("\(s)   \(d)")
        default:
            print("")
        }
        print(lsd)
        // Do any additional setup after loading the view, typically from a nib.
    }

//    (2)switch匹配枚举值
    func chooseMethod(op: Method) -> (Double,Double)->Double {
        switch op {
        case .Add:
            func add(a:Double,b:Double) -> Double{

                return a + b
            }
        case .Sub:
            return {
                (a:Double ,b:Double) -> Double in
                return a - b
            }
        case .Mul:
            return{return $0 * $1}
        case .Div:
            return{$0/$1}

        default:
            return{$0 + $1}
        }

        return {$0 + $1} 

    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值