Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enums with associated values #49

Closed
SNThunderbolt opened this issue Nov 17, 2020 · 2 comments
Closed

enums with associated values #49

SNThunderbolt opened this issue Nov 17, 2020 · 2 comments

Comments

@SNThunderbolt
Copy link

Hi I have this enum with associated types

enum LogValue: Codable, RealmableEnum {
    case sugar(SugarLogCreate)
    case carb(CarbLog)
    case weight(WeightLog)

    init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        if let x = try? container.decode(SugarLogCreate.self) {
            self = .sugar(x)
            return
        }
        if let x = try? container.decode(CarbLog.self) {
            self = .carb(x)
            return
        }
        if let x = try? container.decode(WeightLog.self) {
            self = .weight(x)
            return
        }
        throw DecodingError.typeMismatch(LogValue.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for Log Value"))
    }
    
   func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        switch self {
        case .sugar(let x):
            try container.encode(x)
        case .carb(let x):
            try container.encode(x)
        case .weight(let x):
            try container.encode(x)
        }
    }
}

where each SugarLogCreate, CarbLog and WeightLog are structs conforming toRealmable

I get this error that tells me I have not conformed to RealmableEnums
"Type 'LogValue' does not conform to protocol 'RealmableEnum'"
and makes me put these methods in my code which I have no idea what to do with them:

func rlmValue() -> Any {
        <#code#>
    }
    
    init?(rlmValue: Any) {
        <#code#>
    }
    
    static var rawValueType: Any.Type
    

What should I do in this case?
Thank you in advance for your reply.

@SNThunderbolt
Copy link
Author

just a quick note:
I have added these protocol stubs as follows:

func rlmValue() -> Any {
        switch self {
        case .sugar(let sugarLogCreate): return sugarLogCreate
        case .carb(let carbLog): return carbLog
        case .weight(let weightLog): return weightLog
        }
    }

    init?(rlmValue: Any) {
        if let sugar = rlmValue as? SugarLogCreate {
            self = .sugar(sugar)
        } else if let carb = rlmValue as? CarbLog {
            self = .carb(carb)
        } else if let weight = rlmValue as? WeightLog {
            self = .weight(weight)
        } 
    }

But I have absolutely no idea how deal with the third one:
static var rawValueType: Any.Type

@arturdev
Copy link
Collaborator

Storing enums with associated values is not supported.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants