swift学习:9.枚举

前端之家收集整理的这篇文章主要介绍了swift学习:9.枚举前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1.枚举语法:

enum Platform{
case Windows
case Linux
case MacOs
}
var myPlatform = platform.windows
myPlatform= .Linux
switch myPlatform{
case .Windows:
println(“这是Windows平台”)
default:
println("不支持该平台”)
}

2.关联值

enum Password{
case DigitPassword(Int)
case StringPassword(String)
}
var password = Password.DigitPassword(4078385)
password = Password.StringPassword(“admin”)
switch password{
case .DigitPassword(let digitPwd):
println(“数字密码是:\(digitPwd)”)
case .StringPassword(let strPwd)
println(“字符密码是:\(strPwd)”)
}

3.原始值

enum ASCIIControlCharacter:Character{
case Tab = “\t"
case LineFeed = “\n"
case CarriageReturn = “\t"
}

4.示例代码

enum CompassPoint{

case North

case South

case East

case West

}

var dir = CompassPoint.East

dir = .West

switch dir{

case .North:

print("North")

default:

print("Other")

}


enum Barcode{

case UPCA(Int,Int,Int)

case QRcode(String)

}

var productBarcode = Barcode.UPCA(3,4,8)

productBarcode = .UPCA(3,4,5)

productBarcode = .QRcode("asdf")

switch productBarcode{

case .UPCA(3,5):

println("1")

case var .QRcode(a):

println("2")

default:

println("3")

}


enum Planet:Int{

case Mercury = 1,Venus,Earth,Mars,Jupiter,Saturn,Uranus,Neptune

}

println(Planet.Earth.rawValue)

let possiblePlanet = Planet(rawValue: 7)

//let possiblePlanet = Planet.fromRaw(7)

let planetFind = 9

if let somePlanet = Planet(rawValue: planetFind){

println("Found")

}

else{

println("No Found")

}

猜你在找的Swift相关文章