-1

I am trying to create an inheritance for below enum

enum BankAuthError: String {
    case authFailed = "AuthFailed"
    case technicalError = "Unavailable"
    case accountLocked = "Locked"
    case unknownError = "UnknownError"
    case userInteractionRequired = "UserInteractionNeeded"
    case realmUserAlreadyConnected = "UserExists"
}

I am able to use this enum as below

let errorCode = BankAuthError(rawValue:errorMessageCodeString)

Now I am trying to create inheritance from above struct as below

//MARK:- Enum to handle all sysnc errors
enum SyncErrorStatus: BankAuthError {
 case usernameOrPasswordMissing = "UsernameOrPasswordMissing"
 case signatureMissing = "SignatureMissing"
 case twoPhaseAuthentication = "TwoPhaseAuth"
}

But if I am doing this, I am getting the compiler error as

'SyncErrorStatus' declares raw type 'BankAuthError', but does not conform to RawRepresentable and conformance could not be synthesized

Please let me know whether can we create inheritance from above Raw enum or not.

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
Gagan_iOS
  • 3,638
  • 3
  • 32
  • 51
  • Enums are value types, so they don't support inheritance. Possible duplicate of [Swift enum inheritance](https://stackoverflow.com/questions/33191532/swift-enum-inheritance) – Robert Dresler Apr 02 '19 at 09:42

1 Answers1

6

Enums are value types, so there's no such thing as inheritance for enums. When you declare an enum as enum YourEnum: Type, you declare the rawValue of your enum to be of type Type. However, Type needs to conform to RawRepresentable.

What you are looking for, to create an enum that contains all cases of another enum, plus some other cases in currently not possible in Swift. You cannot inherit all cases of an enum.

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116