Swift enum



enum CompassPoint {
    case north
    case south
    case west
    case east
}


var somePoint = CompassPoint.east


switch somePoint {
case .north:
    print("it's north")
case .south:
    print("it's south")
case .west:
    print("it's west")
case .east:
    print("it's east")
}




enum IdentiCode {
    case barCode(Int,Int,Int,Int)
    case qrCode(String)
    //case barCodeExample1(1,2,var kk:Int) this is invalid
}


var someIdenti = IdentiCode.barCode(8,11,5,1)
someIdenti = IdentiCode.qrCode("xxx")


 var kkk = IdentiCode.barCode // (Int,Int,Int,Int) -> IdentiCode
 
switch someIdenti {
case var .barCode(a,b,c,d):
    print("\(a) and \(b) is \(c) and \(d)")
case .qrCode(let a):
    print(a)
}
 
 // Enum can store raw values but you have to put Type(so you make sure all same type
 
 enum SomeChar:Character {
    case a = "a"
    case b = "\n"
    case c = "\t"
 }
 
 enum Nume : Int {
    case a = 1
    case b = 3
    case c
 }








 
 enum Nume : Int {
    case a = 1
    case b = 3
    case c
 }
 
 var numTest = Nume.c
 print(numTest.rawValue)
 
 let sss = numTest.rawValue
 print(sss)
 
 
let possibleNume = Nume(rawValue:9) // remember it returns optional


if let caseNum = possibleNume {
    switch caseNum {
    case .a:
        print("what the faak")
    default:
        print("yes faak")
    }
}else{
    print("yes sheet")
}
 
// Recursive enumeration


indirect enum Operate {
    case num(Int)
    case plus(Operate,Operate)
    case times(Operate,Operate)
}


let five = Operate.num(5)
let four = Operate.num(4)
let add = Operate.plus(five,four)
let multiply = Operate.times(add,Operate.num(2)) // have to assign specified value


func callOperate(_ Operate:Operate) -> Int {
    switch Operate {
    case .num(let value):
        return value
    case let .plus(left,right):
        return callOperate(left) + callOperate(right)
    case let .times(left,right):
        return callOperate(left) * callOperate(right)
    }
}


print(callOperate(multiply))
print(callOperate(add))
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值