美文网首页
swift-枚举

swift-枚举

作者: malgee | 来源:发表于2018-03-22 18:50 被阅读8次

swift中的枚举不仅能表示整型的枚举, 还可以表示其他类型的枚举(字符串枚举)

可选性的实质就是枚举类型,一个值是nil, 一个值就是定义的类型

// 通过添加:Int 定义枚举是整型的枚举
enum Month: Int {
    case January
    case February
    case March
    case April
    case May
    case June
    case July
    case August
    case September
    case October
    case November
    case December
}

// .June 前面知道是枚举类型, 所以可以省略前面的枚举名称,直接 . 出来后面的枚举值
let currentMonth: Month = Month.June

let currentMonth2: Month = .June

// 整型的枚举 原始值 rawValue
Month(rawValue: 10)  // November
// 字符串类型的枚举, 后面的值可以不写, 默认就是枚举名称, 也可以写出来
enum Language: String {

    case Swift = "swift"
    case ObjectiveC = "Objective-C"
    case HTML
    case CSS
}

let myLanguage: Language = .ObjectiveC

// .rawValue 获取枚举的值
print(myLanguage.rawValue)  // Objective-C
// associate value 相关联的枚举, 可以存放不同类型

enum ATMStatus {

    case Success(Int)
    case Error(String)
}

var balance = 1000

func draw(amount: Int) -> ATMStatus {

    if balance >= amount {
        
        balance -= amount
        
        return .Success(balance)
    }
    else {
    
    return .Error("余额不足")
    }
}

let myStatus = draw(amount: 100)

switch myStatus {
    case let .Success(newValue):
        print("还剩余 \(newValue) 元")
        
    case let .Error(msg):
        print("error: \(msg)")
}

输出: 还剩余 900 元

对于枚举的值的不关心, 可以这么写:
switch myStatus {
case .Success:
    print("成功")
    
case .Error:
    print("失败")  
}

// 补充:

// 多个参数的枚举
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: 10, height: 30)

let Circle = Shape.Circle(centerX: 0, centerY: 0, radius: 10)

let Point = Shape.Point


func area(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: radius):
        
        return M_PI * radius * radius

    case .Point:
        
        return 0
    }

}
// 计算面积
area(shape: square)
area(shape: Rectangle)
area(shape: Circle)
area(shape: Point)

可选性的实质就是枚举

可选性有2个枚举值: `none`、 `some(xx)`
 /// The absence of a value.
    ///
    /// In code, the absence of a value is typically written using the `nil`
    /// literal rather than the explicit `.none` enumeration case.
    case none

    /// The presence of a value, stored as `Wrapped`.
    case some(Wrapped)
`

// 定义一个可选性
var testOption: Optional<String> = Optional.some("malgee")

testOption = .none

switch testOption {
case .none:
    print("nil")
case let .some(string):
    
    print(string)
}

相关文章

  • Swift-枚举

    在 Swift 中,枚举类型是一等(first-class)类型。它们采用了很多在传统上只被类(class)所支持...

  • swift-枚举

    swift中的枚举不仅能表示整型的枚举, 还可以表示其他类型的枚举(字符串枚举) 可选性的实质就是枚举类型,一个值...

  • Swift-枚举

    基础例子 原始值(Raw Value) 关联值(Associate Value) 可选型就是枚举类型 枚举递归

  • Swift-枚举

    1. 枚举的写法 注意枚举成员建议用小写字母开头 2. 原始值 使用同种类型的默认值关联,此默认值成为原始值 使用...

  • Swift-函数、枚举

    最近项目使用的是OC,后头看之前用Swift开发的一个项目时,发现很多细节都忘记了??。为了回忆和以后方便查看,现...

  • Swift-枚举 enum

    本文主要介绍enum的常见使用形式,1、常规枚举写法 2、字符串类型枚举 枚举值和原始值rawValue的关系为c...

  • 跟着洲洲哥一块学习Swift-枚举

    本文首发地址 Swift-枚举 枚举定义了一个通用类型的一组相关的值,使你可以在你的代码中以一个安全的方式来使用这...

  • 跟着洲洲哥一块学习Swift-属性

    本文首发地址 Swift-属性 首先结构体和枚举都是值类型,类是引用类型。 把结构体赋值给一个变量 * 把结构体...

  • Swift-细说枚举(Enum)

    一. 枚举的声明 枚举的定义 声明一个方向的枚举,包含四个枚举值: 东/西/南/北. Swift 的枚举成员在被创...

  • Swift-进阶:枚举enum

    本文主要介绍enum的常见使用形式,以及枚举大小是如何计算的 补充:添加脚本自动生成SIL 通过target ->...

网友评论

      本文标题:swift-枚举

      本文链接:https://www.haomeiwen.com/subject/gaaaqftx.html