Swift学习-枚举(八)

/*

 枚举定义了一个通用类型的一组相关值,使你可以在你的代码中以一种安全的方式来使用这些值。

*/

//定义枚举 "case"关键字定义成员值

enum CompassPoint

{

   case North

   case South

   case East

   case West

}

//定义多个成员值

enum Planet {

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

}


var directionToHead =CompassPoint.West

//一旦directionToHead被声明为CompassPoint,可以使用缩写语法(.

directionToHead = .South


//switch用到枚举的时候,必须包括所有枚举成员,否者要加上default

switch directionToHead

{

   case .North:

     print("Lots of planets have a north")

   case .South:

    print("watch out for penguins")

   case .East:

    print("where the sun rises")

   case .West:

    print("where the skies are blue")

    

}

/*相关值 Swift的枚举存储任何类型的相关值,如果需要的话,每个成员的数据类型可以是各不相同的。

定义一个名为Barcode的枚举类型,它可以是UPCA的一个相关值(IntIntIntInt)元组,

或者是QRCode的一个字符串类型(String)相关值。

这个定义不提供任何IntString的实际值,它只是定义了,

Barcode常量和变量等于Barcode.UPCABarcode.QRCode时,相关值的类型。

*/

enum Barcode {

   case UPCA(Int, Int, Int, Int)

   case QRCode(String)

}

var productBarCode =Barcode.UPCA(8,85909,51226,3)

productBarCode = .QRCode("ABCDEFGHJKLMNOP")

switch productBarCode {

case .UPCA(let numberSystem,let manufacturer,let product, let check):

   print("UPC-A:\(numberSystem),\(manufacturer),\(product),\(check)")

case .QRCode(let productCode):

   print("QR code:\(productCode)")

}

//QR code: ABCDEFGHJKLMNOP


//如果一个枚举成员的所有相关值被提取为常量,或者它们全部被提取为变量,为了简洁,你可以只放置一个var或者let标注在成员名称前:

switch productBarCode {

case let .UPCA(numberSystem,manufacturer,product,check):

   print("UPC-A:\(numberSystem),\(manufacturer),\(product),\(check)")

case let .QRCode(productCode):

   print("QR code:\(productCode)")

}

/*原始值

下面枚举类型的 原始值类型被定义为字符型Character

原始值可以是字符串,字符,或者任何整型值或浮点型值。每个原始值在它的枚举声明中必须是唯一的。

*/

enum ASCIIControlCharacter:Character {

   case Tab ="\t"

   case LineFeed ="\n"

   case CarriageReturn ="\r"

}

/*原始值的隐式赋值

在使用原始值为整数或者字符串类型的枚举时,不需要显式的为每一个成员赋值,这时,Swift将会自动为你赋值。

*/


//当使用整数作为原始值时,隐式赋值的值依次递增1。如果第一个值没有被赋初值,将会被自动置为0

enum Planet1:Int{

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

}

//当使用字符串作为枚举类型的初值时,每个枚举成员的隐式初值则为该成员的名称。

enum CompassPoint1:String{

   case North, South, East, West

}


let earthsOrder =Planet1.Earth.rawValue

print(earthsOrder)//3


let sunsetDirection =CompassPoint1.West.rawValue

print(sunsetDirection)//West

//使用原始值初始化枚举变量 

//返回枚举成员或nil

let possiblePanet =Planet1(rawValue:7)

print(possiblePanet!)//Uranus


let positionToFind =9

if let somePlanet =Planet1(rawValue:positionToFind){

   switch somePlanet{

   case .Earth:

        print("mostly harmless")

    default:

        print("not a safe place for hamans")

    }

}else {

    print("There isn't a planet a position\(positionToFind)")

}

//There isn't a planet a position 9


/*递归枚举

归枚举(recursive enumeration)是一种枚举类型,表示它的枚举中,

有一个或多个枚举成员拥有该枚举的其他成员作为相关值。使用递归枚举时,

编译器会插入一个中间层。你可以在枚举成员前加上indirect(间接)来表示这成员可递归。

*/


enum ArithmeticExpression {

   case Number(Int)

   indirect case Addition(ArithmeticExpression,ArithmeticExpression)

   indirect case Multiplication(ArithmeticExpression,ArithmeticExpression)

}

//可以在枚举类型开头加上indirect关键字来表示它的所有成员都是可递归的:

indirect enum ArithmeticExpression1 {

   case Number(Int)

   case Addition(ArithmeticExpression,ArithmeticExpression)

   case Multiplication(ArithmeticExpression,ArithmeticExpression)

}

func evaluate(expression:ArithmeticExpression) ->Int {

   switch expression{

   case .Number(let value):

       return value

   case .Addition(let left,let right):

       returnevaluate(left) +evaluate(right)

   case .Multiplication(let left,let right):

       returnevaluate(left) *evaluate(right)

    }

}


//计算(5 + 4)* 2

let five = ArithmeticExpression.Number(5)

let four = ArithmeticExpression.Number(4)

let sum = ArithmeticExpression.Addition(five,four)

let product = ArithmeticExpression.Multiplication(sum,ArithmeticExpression.Number(2))

print("evaluate(product):\(evaluate(product))")//evaluate(product):18


//枚举可以包含方法

enum Rank:Int{

   case Ace =1

   case Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten

   case Jack, Queen, King

   func simpleDescription() ->String

    {  

        switch self

        {

           case .Ace:

           return"ace"

           case .Jack:

           return"jack"

           case .Queen:

           return"queen"

           case .King:

           return"king"

           default:

           returnString(self.rawValue)

        }

    }

}

let ace =Rank.Ace

let aceRawValue =ace.rawValue//原始值

print(aceRawValue)//1


let ja =Rank.Jack

let s = ja.simpleDescription()

print("s:\(s)")

//在原始值为3的位置上获取枚举值

if let convertedBank =Rank(rawValue:3)

{

   switch convertedBank

    {

       case .Three:

        print("this is Three")

      default:

         print("this is Other")

    }

   let threeDescription = convertedBank.simpleDescription()

    print("convertedBank:\(convertedBank)")

   print("threeDescription:\(threeDescription)")

}


你可能感兴趣的:(枚举,swift)