快速错误 : 'EnumName' has a raw type that depends on itself

标签 swift xcode enums compiler-errors

我正在开发 Swift iOS 应用程序的旧代码库(由另一位开发人员构建)。上周没有问题,但似乎在上次 XCode 更新 (11.4 (11E146)) 之后,由于出现错误(示例如下),我无法构建项目:

'CarBrand' has a raw type that depends on itself

在整个项目中到处都在使用库 EnumList ......似乎它导致了这个问题。
(https://github.com/polac24/EnumList)

以下是代码的示例:
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"
}


然后在许多地方(例如在表格 View 中)使用此枚举,如下所示:
let brandsList = Array(CarBrand.Values.all)
cell.textLabel?.text = brandsList[indexPath.row].rawValue.value

由于有很多这些错误和一些枚举相对较大我想知道是否有快速修复或解决方法?还是我必须重新编码所有枚举和实现它们的所有文件?

任何帮助将不胜感激!谢谢!

最佳答案

您不需要第三方库来遍历枚举案例(似乎有点矫枉过正)。

只需让您的 CarBrand 枚举使用 CaseIterable协议(protocol),像这样:

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

现在您可以像这样遍历所有枚举:
let allCarBrands = CarBrand.allCases

关于快速错误 : 'EnumName' has a raw type that depends on itself,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61057890/

相关文章:

json - 使用 json API 结果和 NSDictionary 时解包可选值时意外发现 nil

xcode - “pod install”没有将框架添加到我的 Xcode 项目中

ios - 如何从 XCode 中删除 ARC

ruby - 在 ruby​​ 中迭代枚举

ios - 点击 GMSPolygon Swift

swift - 将新图像上传到 Firebase 存储后刷新缓存图像

swift - 在 SwiftUI 中创建惰性 NavigationLink 时出现遗传问题

iphone - CLLocationCooperative2D 到 MKAnnotation

java - 在子类中强制使用 Java 属性类型

java - 为 JHipster 创建通用枚举转换器