具有相同变量的枚举案例

时间:2019-07-04 12:35:15

标签: swift enums

我的代码中有一个枚举,但是大多数情况下需要相同的变量来执行如下操作:

enum Event {   
    case modeActivated(title: String, name: String, category: String, country: String)   
    case modeDeactivated(title: String, name: String, category: String, country: String)   
    case button1Tapped(title: String, name: String, category: String, country: String)   
    case button2Tapped(title: String, name: String, category: String, country: String) 
}

目前还可以。但是,当我需要对这些参数执行如下操作时,会导致很多代码重复。

extension SomeEvent: Event {
var parameters: [String: Any] {
    var parameters: [String: Any] = [:]

        switch self {

        case let .modeActivated(title, name, category, country):
              parameters[ParameterKeys.contentTitle.rawValue] = title
              parameters[ParameterKeys.sourceName.rawValue] = name
              parameters[ParameterKeys.sourceCategory.rawValue] = category
              parameters[ParameterKeys.sourceCountry.rawValue] = country
        case let .modeDeactivated(title, name, category, country):
              parameters[ParameterKeys.contentTitle.rawValue] = title
              parameters[ParameterKeys.sourceName.rawValue] = name
              parameters[ParameterKeys.sourceCategory.rawValue] = category
              parameters[ParameterKeys.sourceCountry.rawValue] = country
        case let .button1Tapped(title, name, category, country):
              parameters[ParameterKeys.contentTitle.rawValue] = title
              parameters[ParameterKeys.sourceName.rawValue] = name
              parameters[ParameterKeys.sourceCategory.rawValue] = category
              parameters[ParameterKeys.sourceCountry.rawValue] = country
        case let .button2Tapped(title, name, category, country):
              parameters[ParameterKeys.contentTitle.rawValue] = title
              parameters[ParameterKeys.sourceName.rawValue] = name
              parameters[ParameterKeys.sourceCategory.rawValue] = category
              parameters[ParameterKeys.sourceCountry.rawValue] = country
        }
    }
}

有什么办法可以避免这种情况?

2 个答案:

答案 0 :(得分:1)

您可以像这样组合enum cases

var parameters: [String: Any] {
    var parameters: [String: Any] = [:]

    switch self {
    case let .modeActivated(title,name,category,country),
         let .modeDeactivated(title,name,category,country),
         let .button1Tapped(title,name,category,country),
         let .button2Tapped(title,name,category,country):

        parameters[ParameterKeys.contentTitle.rawValue] = title
        parameters[ParameterKeys.sourceName.rawValue] = name
        parameters[ParameterKeys.sourceCategory.rawValue] = category
        parameters[ParameterKeys.sourceCountry.rawValue] = country
    }

    return parameters
}

另外,请说明SomeEvent是什么。因为您添加的代码未编译。

答案 1 :(得分:0)

您可以为枚举创建一个计算所得的属性,例如

enum Event {
    case modeActivated(title: String, name: String, category: String, country: String)
    case modeDeactivated(title: String, name: String, category: String, country: String)
    case button1Tapped(title: String, name: String, category: String, country: String)
    case button2Tapped(title: String, name: String, category: String, country: String)

    var title:String {
        switch (self) {
        case .modeActivated(let title, _, _, _),
             .modeDeactivated(let title, _, _, _),
             .button1Tapped(let title, _, _, _),
             .button2Tapped(let title, _, _, _):
                return title
        }
    }

    // and so on for the other values like name, category and country
}

然后像访问它们

var e:Event = .modeActivated(title: "hello", name: "name", category: "cat", country: "country")
var parameters: [String: Any] = [:]
parameters[ParameterKeys.contentTitle.rawValue] = e.title