Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

enums

swift

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?

like image 261
iPhoneDeveloper Avatar asked Aug 04 '17 09:08

iPhoneDeveloper


Video Answer


1 Answers

From Docs

In particular, the raw-value type must conform to the Equatable protocol and one of the following protocols: ExpressibleByIntegerLiteral for integer literals, ExpressibleByFloatLiteral for floating-point literals, ExpressibleByStringLiteral for string literals that contain any number of characters, and ExpressibleByUnicodeScalarLiteral or ExpressibleByExtendedGraphemeClusterLiteral for string literals that contain only a single character.

So make your class Abc to conform to Equatable and one of the above mentioned protocols. Here is an example

public class Abc : Equatable,ExpressibleByStringLiteral{
    var age  = 25
    var name = "Abhi"
    public static func == (lhs: Abc, rhs: Abc) -> Bool {
        return (lhs.age == rhs.age && lhs.name == rhs.name)
    }
    public required init(stringLiteral value: String) {
        let components = value.components(separatedBy: ",")
        if components.count == 2 {
            self.name = components[0]
            if let age = Int(components[1]) {
                self.age = age
            }
        }
    }
    public required convenience init(unicodeScalarLiteral value: String) {
        self.init(stringLiteral: value)
    }
    public required convenience init(extendedGraphemeClusterLiteral value: String) {
        self.init(stringLiteral: value)
    }
}

enum TestEnum : Abc {
    case firstCase = "Jack,29"
    case secondCase = "Jill,26"
}

Now you can initialize your enum like

let exEnum = TestEnum.firstCase
print(exEnum.rawValue.name) // prints Jack

For detailed discussion and example you can refer https://swiftwithsadiq.wordpress.com/2017/08/21/custom-types-as-raw-value-for-enum-in-swift/

like image 122
Mohammad Sadiq Avatar answered Oct 03 '22 22:10

Mohammad Sadiq