How to declare enums in Swift of a particular class type?

Viewed 14085

I am trying to create a class and use that class as the type for my new enum like shown below.

class Abc{
    var age  = 25
    var name = "Abhi"
}

enum TestEnum : Abc {
    case firstCase
    case secondCase
}

I am getting following error in playground .

error: raw type 'Abc' is not expressible by any literal

So i tried conforming to RawRepresentable protocol like this.

extension TestEnum : RawRepresentable{
    typealias RawValue = Abc

    init?(rawValue:RawValue ) {
        switch rawValue {
        case Abc.age :
            self = .firstCase

        case Abc.name :
            self = .secondCase
        }
    }

    var rawValue : RawValue {
        switch self{

        case .firstCase :
            return Abc.age

        case .secondCase :
            return Abc.name
        }
    }
}

I am getting following errors after this :

error: raw type 'Abc' is not expressible by any literal
error: instance member 'age' cannot be used on type 'Abc'
error: instance member 'name' cannot be used on type 'Abc'

What is the proper way to declare enums of a certain class type, not getting clear idea on this. Anyone help?

3 Answers
Related