Swift Error: 'EnumName' has a raw type that depends on itself

I am working on a old code base of a Swift iOS app (build by another developer). Last week there were no issues, but seems like after the last XCode update (11.4 (11E146)) I am unable to build the project because of an error (example given below):

'CarBrand' has a raw type that depends on itself

Everywhere throughout the project the library EnumList is being used... it seems like it is causing the issue. (https://github.com/polac24/EnumList)

Here is an example of what the code looks like:

import Foundation
import EnumList

enum CarBrand: EnumListStringRaw<CarBrand.Values>, RawRepresentable {
    struct Values: StringEnumValues {
        typealias Element = CarBrand
        static var allRaws:Set<String> = []
    }

    case AUD = "Audi AG"
    case BMW = "Bayerische Motoren Werke AG"
    case MERC = "Mercedes-Benz"
}

This enum is then used in a number of places (for example in a table view) like so:

let brandsList = Array(CarBrand.Values.all)
cell.textLabel?.text = brandsList[indexPath.row].rawValue.value

Since there are a lot of these errors and some of the enums are relatively large I was wondering if there is a quick fix or a workaround for this? Or will I have to re-code all of the enums and all the files that implement them?

Any help would be greatly appreciated! Thanks!

Upvotes: 0

Views: 267

Answers (2)

I received this solution from the EnumList library creator on GitHub and thought posting it here may help someone else along the way. This solution is very quick and does not require any code rewriting for the enums or their implementations.

import Foundation
import EnumList

extension CarBrand{}

enum CarBrand: EnumListStringRaw<CarBrand.Values>, RawRepresentable {
    struct Values: StringEnumValues {
        typealias Element = CarBrand
        static var allRaws:Set<String> = []
    }

    case AUD = "Audi AG"
    case BMW = "Bayerische Motoren Werke AG"
    case MERC = "Mercedes-Benz"
}

Simply adding the empty "extension CarBrand{}" seems to fix the Swift compiler issue!

Link to the original answer I received on GitHub: https://github.com/polac24/EnumList/issues/5

Thank you to library creator for the support!

Upvotes: 0

johnny
johnny

Reputation: 1733

You don't need a third party library to iterate through enum cases (seems like overkill).

Just have your CarBrand enum use the CaseIterable protocol, like this:

enum CarBrand: String, RawRepresentable, CaseIterable {
    case AUD = "Audi AG"
    case BMW = "Bayerische Motoren Werke AG"
    case MERC = "Mercedes-Benz"
}

Now you can go through all enums like this:

let allCarBrands = CarBrand.allCases

Upvotes: 1

Related Questions