简体   繁体   English

Swift在没有开关的情况下获取枚举中的关联值

[英]Swift get associated value in enums without switch

I have enum: 我有枚举:

enum RetailDemandReturnOperation {
    case salesReturn(value: MSRetailSalesReturnRealm)
    case demand(value: MSRetailDemandRealm)
}

MSRetailDemandRealm and MSRetailDemandRealm are both implement same protocol, that have variables title and stats. MSRetailDemandRealmMSRetailDemandRealm都实现了相同的协议,它具有变量title和stats。 I want to extract this values, but i don't care of which object actually stored in. Consider following: 我想提取这些值,但我不关心实际存储的是哪个对象。请考虑以下内容:

 switch data! {
        case .salesReturn(let object):
            titleString = object.title
            statistics = object.stats
        case .demand(let object):
            titleString = object.title
            statistics = object.stats
          break
        }

I have to go in each enum value to get property of protocol. 我必须进入每个枚举值以获得协议的属性。 Is any way i can do it shorter and cleaner? 我有什么方法可以做得更短更清洁? Get associated value, no matter what it is, as long as it conforms to my protocol, and get protocol values? 获取相关值,无论它是什么,只要它符合我的协议,并获得协议值? Thanks. 谢谢。

You could add a property to your enum that returns the protocol. 您可以向枚举中添加一个返回协议的属性。 For example: 例如:

enum RetailDemandReturnOperation {
    case salesReturn(value: MSRetailSalesReturnRealm)
    case demand(value: MSRetailDemandRealm)

    var realm: MSRetailRealm {
        switch self {
        case .salesReturn(let realm):
            return realm
        case .demand(let realm):
            return realm
        }
    }
}

Then, when you want to access those properties on a specific value of the enum, just use: 然后,当您想要在枚举的特定值上访问这些属性时,只需使用:

let operation = RetailDemandReturnOperation.salesReturn(value: MSRetailSalesReturnRealm())
let title = operation.realm.title

Since RetailDemandReturnOperation always has the associated value for MSRetailRealm , you can give it a new property of type RetailDemandReturnOperation . 由于RetailDemandReturnOperation始终具有RetailDemandReturnOperation的关联值, MSRetailRealm您可以为其提供RetailDemandReturnOperation类型的新属性。 Then, you can get rid of the the associated value in your enum. 然后,您可以删除枚举中的相关值。

enum RetailDemandReturnOperation {
    case salesReturn
    case demand
}

protocol MSRetailRealm {
    var stats: Int { get set }
    var title: String { get set }
    var operation: RetailDemandReturnOperation { get }
}

struct MSRetailDemandRealm: MSRetailRealm {
    //.. your old code
    var operation: RetailDemandReturnOperation { return .demand }
}

struct MSRetailSalesReturnRealm: MSRetailRealm {
    //.. your old code
    var operation: RetailDemandReturnOperation { return .salesReturn }
}

Now you can access stats and title no matter what operation it is. 现在,您可以访问statstitle无论它是什么操作。 And if you care about the operation, simply access the operation property. 如果您关心操作,只需访问operation属性即可。

func example(object: MSRetailRealm) {
    let titleString = object.title
    switch object.operation {
        case .salesReturn:
            break
        case .demand:
            break
    }
}

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

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