Swfit:枚举专题

1. 基本语法以及原始值

import UIKit

enum Month: Int { // Month这个枚举型和整数关联起来,等号后边的值就是枚举型的rawValue
    case January = 1 // 其实就写这一样就够了,下面会自动变成2...12
    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 monthBeforeNewYear(month: Month) -> Int {
    return 12 - month.rawValue
}

let month = Month.May
monthBeforeNewYear(month: month)

// 通过rawValue找到对应的枚举值
let value = 1
if let month2 = Month(rawValue: value) { // 返回January,这里的month是可选型
    print(month2)
}

enum Direction: Int { // 首先这里一定要申请是Int类型的,然后不标rawValue默认按顺序从0开始
    case East, South, West, North
}

Direction.West.rawValue // 返回2

// 如果枚举型是String类型的,啥也不关联,默认就是本身的字符串

2. 枚举和关联值得应用

import UIKit

// Associate Value
enum ATMStatus {
    case Success(Int) // Success这个枚举值和一个整数相关联
    case Error(String) // Error这个枚举值和一个字符串相关联
}

var curMoney = 1000 // 当前的钱

func withdraw(_ amount: Int) -> ATMStatus {
    if curMoney >= amount {
        curMoney -= amount
        return .Success(curMoney) // 返回状态顺便把值也捎上了
    } else {
        return .Error("Not having enough money") // 同上
    }
}

let res = withdraw(800) // 返回一个带有关联值得枚举型
switch res {
case let .Success(curMoney): // 这里要获取捎上的值,要用let进行解包
    print(curMoney)
case let .Error(str): // 同理进行解包
    print(str)
}

标注:

- 原始值和关联值是互斥的,二者择其一!

- 关联值可以关联多个值,可以多于一个

代码如下

import UIKit

// Associate Value
enum Shape {
    case Square(side: Double)
    case Rectangle(width: Double, height: Double)
    case Circle(cenetrX: Double, centerY: Double, radius: Double)
    case Point
}

let rect = Shape.Rectangle(width: 10, height: 20) // 这里关联了两个变量,本质是关联了一个元组

func getArea(shape: Shape) -> Double { // 根据形状返回面积
    switch shape {
    case let .Square(side: side): // 再次用到解包的概念
        return side * side
    case let .Rectangle(width: width, height: height):
        return width * height
    case let .Circle(_, _, radius): // 计算圆的面积不需要知道前两个,只用解包最后一个半径就可以了
        return Double.pi * radius * radius //圆周率的π是这个哦
    default:
        return 0
    }
}

3. 如果枚举中含有递归定义,就要在声明enum的时候,在enum之前加上indirect关键字

4. 枚举也可以有方法

import UIKit

// Associate Value
enum Shape {
    case Square(side: Double)
    case Rectangle(width: Double, height: Double)
    case Circle(cenetrX: Double, centerY: Double, radius: Double)
    case Point
    
    func getArea() -> Double { // 根据形状返回面积,这里不需要传参了
        switch self { // switch self第一次见
        case let .Square(side: side): // 再次用到解包的概念
            return side * side
        case let .Rectangle(width: width, height: height):
            return width * height
        case let .Circle(_, _, radius): // 计算圆的面积不需要知道前两个,只用解包最后一个半径就可以了
            return Double.pi * radius * radius //圆周率的π是这个哦
        default:
            return 0
        }
    }
    
}

let rect = Shape.Rectangle(width: 10, height: 20) // 这里关联了两个变量,本质是关联了一个元组
rect.getArea() // 200

 

猜你喜欢

转载自blog.csdn.net/shijie97/article/details/83994159