简体   繁体   English

如何在Swift中将多个值与枚举关联?

[英]How to associate multiple values with enums in Swift?

I'm trying to use an enum to categorize the various sections in a UITableView . 我正在尝试使用一个枚举对UITableView的各个部分进行分类。

Here's the current implementation of the Section enum : 这是Section enum的当前实现:

enum Section: Int, CustomStringConvertible {

    case open = 0
    case closed = 1

    var description: String {
        switch self {
        case .open:
            return "Open"
        case .closed:
            return "Closed"
        }
    }
}

Here's how I'm using it to name the titles for the headers in my UITableView sections : 这是我使用它命名UITableView部分中标题标题的方式:

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return Section(rawValue: section)?.description
}

I'm just curious to know if there's a better way of implementing this. 我只是想知道是否有更好的方法来实现这一点。 Perhaps with tuples. 也许与元组。 I'm not exactly sure, but I'd love to see if there's a better solution. 我不确定,但是我很想看看是否有更好的解决方案。

This is just one use case and the two cases given are just for brevity. 这只是一个用例,给出的两个用例只是为了简洁。 I'd like to use multiple values, for instance UIColors associated with the given case . 我想使用多个值,例如与给定case相关联的UIColors

If all you're trying to do is set up a correspondence between numbers and strings: 如果您要做的只是在数字和字符串之间建立对应关系:

0 -> Open
1 -> Closed

... then it's hard to see what the enum is for. ...那么很难看到枚举的用途。 What you want is a dictionary where the numbers are keys, or even just a simple array : 您想要的是一本字典 ,其中数字是键,甚至只是一个简单的数组

let arr = ["Open", "Closed"]

In the array, the string is indexed by the numbers: 在数组中,字符串由数字索引:

return arr[rawValue: section]

If you are going to use the enum to provide other information for the header (eg. background color, or whatever) you could add more vars to the enum as you did with the description. 如果要使用枚举为标头提供其他信息(例如,背景色等),则可以像对说明一样向枚举添加更多的变量。

But if you are only using for header titles you are probably better with an array: 但是,如果仅将标题用作标题,则使用数组可能会更好:

let sectionTitles = ["Open", "Closed"] //var if the section can change.

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return sectionTitles[section]
}

Assuming you will be adding more variables or functionality to this enum, your syntax is fine. 假设您将向此枚举添加更多变量或功能,那么语法就可以了。 You can consolidate a bit when naming the cases like this: 您可以在命名如下情况时进行合并:

case open, closed, thirdCase, fourthCase

If your enum has a rawValue of Int, and the cases start from 0 and number incrementally, you don't have to assign the values. 如果您的枚举的rawValue为Int,并且个案从0开始并递增编号,则不必分配值。

If you wanted the increments to start at a different number than 0, you can set it up like so: 如果您希望增量以不同于0的数字开头,则可以这样设置:

case open = 3, closed, thirdCase, fourthCase

Your cases would then have a rawValues of 3, 4, 5, 6 您的案例的原始值将为3、4、5、6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM