简体   繁体   English

Swift - 嵌套枚举默认值

[英]Swift - Nested Enums default values

I have an enum as follows我有一个枚举如下

enum AccountForm: String {

    case Profile

    enum Content: String {
        case Feedback
        case Likes
    }

    enum Actions: String {
        case Redeem
        case Help
    }
}

This represents a form, where profile content and actions are sections and the cases are rows.这表示一个表单,其中profile contentactions是部分,案例是行。

These resolve to strings and work as expected这些解析为字符串并按预期工作

AccountForm.Profile.rawValue returns "Profile" AccountForm.Profile.rawValue返回“配置文件”

AccountForm.Content.Feedback.rawValue returns "Feedback" AccountForm.Content.Feedback.rawValue返回“反馈”

However, I'd like AccountForm.Content.rawValue to return "Content"但是,我希望AccountForm.Content.rawValue返回“内容”

Is this possible?这可能吗? Or is there a better way besides enums to achieve this?或者除了枚举之外还有更好的方法来实现这一目标吗?

I'm guessing you've got an answer to this by now but just in case you didn't try this:我猜你现在已经有了答案,但以防万一你没有尝试这个:

enum AccountForm : String {
    case profile

    enum Content: String {
        static let rawValue = "Content"

        case feedback = "Feedback"
        case likes = "Likes"
    }

    enum Actions : String {
        static let rawValue = "Actions"

        case redeem = "Redeem"
        case help = "Help"
    }
}

Static properties on both the Content and Actions enumerations should achieve what you want. ContentActions枚举上的静态属性应该可以实现您想要的。 A word of warning though.警告的话。 By calling the properties rawValue you're obviously implying the returned values are raw values when technically they aren't.通过调用属性rawValue您显然暗示返回的值是原始值,而从技术上讲它们不是。 If you can I'd think of a better name (maybe sectionTitle ?).如果可以的话,我会想一个更好的名字(也许是sectionTitle ?)。

Couple of other things to note.还有一些需要注意的事情。

First, you have to define the properties as static as it sounds like you want to call them on the enumeration type (eg AccountForm.Content.rawValue ) rather than on an individual enumeration case (eg AccountForm.Content.feedback.rawValue ).首先,您必须将属性定义为static ,因为听起来您想在枚举类型(例如AccountForm.Content.rawValue )而不是在单个枚举案例(例如AccountForm.Content.feedback.rawValue )上调用它们。 I'll leave you to decide whether that makes sense in your context.我会让你决定这在你的上下文中是否有意义。

Secondly, when Swift 3.0 arrives, the recommendation for enumeration cases is going to be that case labels follow a lowerCamelCase naming convention rather than the UpperCamelCase convention that was recommended in Swift 2.2.其次,当 Swift 3.0 到来时,对枚举案例的建议将是案例标签遵循lowerCamelCase命名约定,而不是Swift 2.2 中推荐的UpperCamelCase约定。

I've followed the Swift 3.0 recommendation here but the result is that explicit raw-value assignments is needed as you won't be able to rely on using the implicit raw-value assignment mechanism assigning a string with an UpperCamelCase representation which is kind of annoying but those are the implications.我在这里遵循了 Swift 3.0 建议,但结果是需要显式的原始值分配,因为您将无法依赖使用隐式原始值分配机制来分配具有UpperCamelCase表示的字符串,这是一种烦人,但这些是影响。

Anyway, hope it helps.无论如何,希望它有所帮助。

enum Typo {
    case Bold
    case Normal
    case Italic
    case All
}
enum Format: CustomStringConvertible {
    case Header(Typo)
    case Text(Typo)
    var description:String {
        switch self {
        case .Header(let value) where value != .All:
            return "Header.\(value)"
        case .Header(let value) where value == .All:
            return "Header"
        case .Text(let value) where value == .All:
            return "Text"
        case .Text(let value) where value != .All:
            return "Text.\(value)"
        default:
            return ""
        }
    }
}

let a:Format = .Header(.Bold)
let b:Format = .Text(.Italic)

Format.Header(.All)   // Header
Format.Text(.Bold)    // Text.Bold
Format.Text(.All)     // Text

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

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