Swift 枚举的几个小用法

栏目: Swift · 发布时间: 4年前

内容简介:在 Swift 中,枚举是一个非常方便也非常强大的类型。我们在日常使用中也经常会使用到它。例如,我们最常见的 optional:这里不准备介绍枚举的基本用法,只是记录两个比较好用的枚举用法。

在 Swift 中,枚举是一个非常方便也非常强大的类型。我们在日常使用中也经常会使用到它。

例如,我们最常见的 optional:

enum Optional<T> {
  case Some(T)
  case None
}
复制代码

这里不准备介绍枚举的基本用法,只是记录两个比较好用的枚举用法。

关联值

关联值是将额外信息附加到 enum case 中的一种极好的方式。

例如,当我们需要将一系列的值传到下一个类中时,一般情况下我们像下方代码一样写出几个设置的方法:

struct MyStruct {
    var value: Int

    init(_ value: Int?) {
        if let val = value {
            self.value = val
        } else {
            self.value = Int(INT_MAX)
        }
    }
}

class Two {
    var value1: String?
    var value2: Int?
    var value3: MyStruct?

    func setValue1(value: String?) { }
    func setValue2(value: Int?) { }
    func setValue2(value: MyStruct?) { }
}
复制代码

这样当需要传的值变多时,代码无疑就会变得没那么好看了。我们可以用枚举来简化:

enum ValueBind {
    case bindStringValue(str: String)
    case bindIntValue(num: Int)
    case bindModel(model: MyStruct)
}

class Two {
    var value1: String?
    var value2: Int?
    var value3: MyStruct?

    func setValueBind(value: ValueBind) {
        switch value {
        case .bindStringValue(let str):
            print(str)
        case .bindModel(let model):
            print(model.value)
        case .bindIntValue(let num)
            print(num)
        }
    }
}
复制代码

利用枚举关联值之后,咱们的代码马上就简洁了不少。

自定义枚举类型

平常我们使用枚举时,我们在为枚举定义 value 时,一般就只用了几种基本的类型:

enum Direction {
    case left
    case top
    case right
    case bottom
}

enum StringEnum: String {
    case hello = "hello"
    case world = "world"
}

enum IntEnum: Int {
    case one = 1
    case two = 2
}
复制代码

但是,如果我们需要在枚举类型放入我们自定义的类型的话,我们就需要为枚举加一些东西了。

enum CustomEnum: RawRepresentable {
    typealias RawValue = MyStruct

    case null
    case one
    case two
    
    init?(rawValue: MyStruct) {
        switch rawValue.value {
        case 1:
            self = .one
        case 2:
            self = .two
        default:
            self = .null
        }
    }

    var rawValue: MyStruct {
        switch self {
        case .one:
            return MyStruct(1)
        case .two:
            return MyStruct(2)
        default:
            return MyStruct(nil)
        }
    }
}
复制代码

我们让枚举遵守 RawRepresentable 协议,并实现协议的一些属性及方法:

/*
    将枚举的 RawValue 关联为自己希望的类型
**/
associatedtype RawValue

/*
    利用自己关联的类型生成枚举的实例
**/
init?(rawValue: Self.RawValue)

/*
    将自己定义的类型的作为 RawValue 返回
**/
var rawValue: Self.RawValue { get }
复制代码

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Algorithms

Algorithms

Robert Sedgewick、Kevin Wayne / Addison-Wesley Professional / 2011-3-19 / USD 89.99

Essential Information about Algorithms and Data Structures A Classic Reference The latest version of Sedgewick,s best-selling series, reflecting an indispensable body of knowledge developed over the ......一起来看看 《Algorithms》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具