枚举

enum Month{
    case January
    case February
    case March
    case April
    case May
    case June
    case July
    case August
    case September
    case October
    case November
    case December
}
enum Season{
    case Spring, Summer, Autumn, Winter
}
let curMonth = Month.June
let curMonth2: Month = .June
func season(month: Month) ->Season{
    switch month{//里面的Month都可以去掉,但是点不能去掉
    case Month.March, Month.April, Month.May:
        return Season.Spring
    case Month.June, Month.July, Month.August:
        return .Summer
    case Month.September, Month.October, Month.November:
        return .Autumn
    case Month.December, Month.January, Month.February:
        return .Winter
    }
}
season(curMonth)

原始型Raw Value

enum Month:Int{
    case January = 1
    case February = 2
    case March = 3
    case April = 4
    case May = 5
    case June = 6
    case July = 7
    case August = 8
    case September = 9
    case October = 10
    case November = 11
    case December = 12
}
func monthsBeforeNewYear(month: Month) ->Int{
    return 12 - month.rawValue
}
let month = Month.April
monthsBeforeNewYear(month)
let input = 4
//let month2 = Month(rawValue: input)
//if let month2 = month2{
//    monthsBeforeNewYear(month2)
//}
if let month2 = Month(rawValue: input){
    monthsBeforeNewYear(month2)
}

enum Grade: Int{
    case F,E,D,C,B,A
}
let grade: Grade = .A
print("Your score is \(grade.rawValue)")

enum Coin: Int{
    case Penny = 1
    case Nickel = 5
    case Dime = 10
    case Quarter = 25
}
let coin: Coin = .Quarter
print("It's \(coin.rawValue) cents")

enum ProgrammingLanguage: String{
    case Swift = "Swift"
    case ObjectiveC = "Objective-C"
    case C = "C"
    case Java = "Java"
}
let myFavoriteLanguage:ProgrammingLanguage = ProgrammingLanguage.Swift
print("\(myFavoriteLanguage.rawValue) is my favorite language")

Associate Value 关联值

enum AtmStatus{
    case Success(Int)//关联不同的类型
    case Error(String)
    case Waiting
}
var balance = 1000
func withdrw(amount:Int) ->AtmStatus{
    if balance >= amount{
        balance -= amount
        return AtmStatus.Success(balance)
    }
    else{
        return AtmStatus.Error("Not enough money")
    }
}
let result = withdrw(100)
switch result{
case let .Success(newBalance):
    print("\(newBalance) yuan left in your account")
case let .Error(errorMessage):
    print("Error:\(errorMessage)")
case .Waiting:
    print("Waiting")
}

switch result{
case .Success:
    print("Success")
case .Error:
    print("Error")
case .Waiting:
    print("Waiting")
}

enum Shape{
    case Square(side: Double)
    case Rectangle(width: Double, height: Double)
    case Circle(centerx:Double, centery:Double, radius:Double)
    case Point
}
let square = Shape.Square(side: 10)
let rectangle = Shape.Rectangle(width: 20, height: 30)
let circle = Shape.Circle(centerx: 0, centery: 0, radius: 15)
let point = Shape.Point

func area(shape: Shape) ->Double{
    switch shape {
    case let .Square(side):
        return side*side
    case let .Rectangle(width, height):
        return width*height
    case let .Circle(_,_,r)://使用_忽略没用的参数
        return M_PI*r*r
    case .Point:
        return 0
    }
}
area(square)
area(rectangle)
area(circle)
area(point)

可选型的实质是枚举

var age: Int? = 12
print(age)
age = nil

var website: Optional = Optional.Some("imooc.com")
website = .None
website = "imooc.com"
//website = nil

switch website{
case .None:
    print("No website")
case let .Some(website):
    print("The website is \(website)")
}

枚举递归,前面要有indirect关键字

indirect enum ArithmeticExpression{
    case Number(Int)
    case Addition(ArithmeticExpression, ArithmeticExpression)
    case Multiplication(ArithmeticExpression, ArithmeticExpression)
}
let five = ArithmeticExpression.Number(5)
let four = ArithmeticExpression.Number(4)
let sum = ArithmeticExpression.Addition(five, four)
let two = ArithmeticExpression.Number(2)
let product = ArithmeticExpression.Multiplication(sum, two)

func evaluate(expression:ArithmeticExpression) ->Int{
    switch expression{
    case let .Number(value):
        return value
    case let .Addition(left, right):
        return evaluate(left) + evaluate(right)
    case let .Multiplication(left, right):
        return evaluate(left) * evaluate(right)
    }
}
evaluate(product)

在枚举中定义方法

enum Shape{
    case Square(side: Double)
    case Rectangle(width: Double, height: Double)
    case Circle(centerx:Double, centery:Double, radius:Double)
    case Point
    func area() ->Double{
        switch self {
        case let .Square(side):
            return side*side
        case let .Rectangle(width, height):
            return width*height
        case let .Circle(_,_,r)://使用_忽略没用的参数
            return M_PI*r*r
        case .Point:
            return 0
        }
    }
}
let square = Shape.Square(side: 10)
let rectangle = Shape.Rectangle(width: 20, height: 30)
let circle = Shape.Circle(centerx: 0, centery: 0, radius: 15)
let point = Shape.Point
square.area()
rectangle.area()
circle.area()
point.area()

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