简体   繁体   中英

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. 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 . 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. And if you care about the operation, simply access the operation property.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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