简体   繁体   中英

Convert enum from one module to another module with same enum name

My project is using framework and both framework and project are using same enum values. But when I passing param from Main app to module controller I'm getting that they are not same so way to do it is to convert it. I create extension:

extension Result {
    init(_ data: Messaging.MessagingResult) {
        switch data {
            case .authenticationFailed:     self = .authenticationFailed
            case .connectionError:          self = .connectionError
        }
    }
}

and now I have to create function which will take project enum back to module enums

 let adaptedConsume = transform(from: self.consume)

Is this right approach and how transform function should look like

I don't know if I understood your question, but supposing that what you want to do is to map the values of one enum to another, you can just use typealias to do that.

Swift makes it a lot to map types from Objective C to Swift.

Something like typealias Result = Messaging.MessagingResult should work.

You can know more about typealis from Swift Documentation .

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