Home > Enterprise >  Swift enum conformance to identifiable: Type doesn't conform to Identifiable protocol
Swift enum conformance to identifiable: Type doesn't conform to Identifiable protocol

Time:03-02

I have an enum with associated values, which I want to use as an item in RxDataSources. I tried conforming it to identifiable by conforming it to Hashable like below

enum DriverHubWidget: Hashable, Identifiable {
    static func == (lhs: DriverHubWidget, rhs: DriverHubWidget) -> Bool {
        return lhs.hashValue == rhs.hashValue
    }
    
    var id: Int { hashValue }
    
    case greetings(DriverHubGreetingsViewModel)
    case scorecard(DriverHubScorecardSummary?, Error?)
    case optOut
    
    func hash(into hasher: inout Hasher) {
        switch self {
        case .greetings( _):
            return hasher.combine(1)
        case .scorecard( _, _):
            return hasher.combine(2)
        case .optOut:
            return hasher.combine(3)
        }
    }
}

I implemented the hasher function by simply assigning each case an Int value. Then to conform to identifiable, I added an id property that returns the hashValue. This compiles just fine.

Now when I try to use this to declare a type alias for the section model, like below

typealias WidgetSection = AnimatableSectionModel<String, DriverHubWidget>

It does compile and throws the error, Type 'DriverHubWidget' does not conform to protocol 'IdentifiableType'

I can't understand why it doesn't work, it compiles fine when the enumis comformed to Hashable and Identifiable, but when used the conformance somehow is invalid Is it because the associated values for the enums re not Hashable?

CodePudding user response:

You have confused Identifiable, a Swift built-in protocol, with IdentifiableType, a protocol in the RxDataSource library.

You can just conform to IdentifiableType.

enum DriverHubWidget: Hashable, IdentifiableType {
    
    var identity: Int {
        hashValue
    }

    ...
}

The way you conform to Hashable seems weird to me though. You are considering two values of the enum equal as long as they are the same case, and disregarding their associated values. That is, .greeting(x) == .greeting(y) would be true. This seems rather counterintuitive. If this is really what you want for identity, you might want to just implement identity this way:

var identity: Int {
    switch self {
    case .greetings( _):
        return 1
    case .scorecard( _, _):
        return 2
    case .optOut:
        return 3
    }
}

and conform to Hashable by actually taking the associated values into account, or not conform to Hashable at all.

  • Related