Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

349
Visualizações
Enum with associated value conforming to CaseIterable, RawRepresentable

I'm trying to make enum with associated value conform to CaseIterable, RawRepresentable. I'm fine with some default values for associated values when initializing by rawValue.

enum GenresAssociated: CaseIterable, RawRepresentable, Equatable {
    case unknown(String)
    case blues(String)
    case classical(String)
    // Implementing CaseIterable
    typealias AllCases = [GenresAssociated]
    // Enums can have no storage, but the class they are in CAN. Note 'static' in declaration
    static var allCases: [GenresAssociated] = [.unknown(""), .blues(""), .classical("")]


    typealias RawValue = Int
    var rawValue: Int {

        // MARK: This causes a crash for unknown reason
        return GenresAssociated.allCases.firstIndex(where: {  if case self = $0 { return true } else { return false } } ) ?? 0
    }
    init?(rawValue: Int) {
        guard GenresAssociated.allCases.indices.contains(rawValue) else { return nil }
        self = GenresAssociated.allCases[rawValue]
    }
}

Is there any way to make it without switching manually by all the cases, i.e. without such code:

    typealias RawValue = Int
    var rawValue: Int {
        switch self {
        case .unknown:
            return 0
        case .blues:
            return 1
        case .classical:
            return 2
        }
    }

What's notable is that very similar code works just fine, for example

enum EnumWithValue {
    case one(NSString!), two(NSString!), three(NSString!)
}

let arrayOfEnumsWithValues: [EnumWithValue] = [.one(nil), .two(nil), .three("Hey")]

if let index = arrayOfEnumsWithValues.firstIndex(where: { if case .two = $0 { return true }; return false }) {
    print(".two found at index \(index)") //prints ".two found at index 1"
}
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

I got it finally to work with Mirror!

var rawValue: Int {
    let selfCaseName = Mirror(reflecting: self).children.first!.label!

    return GenresAssociated.allCases.firstIndex(where: { (genre) in
        let switchingCaseName = Mirror(reflecting: genre).children.first!.label!

        return switchingCaseName == selfCaseName
    })!
}

Don't mind the force unwrapping, it is safe here.

over 4 years ago · Santiago Trujillo Relatório

0

This parameter will work with enums containing both rawValue cases and cases with associatedValues:

var caseName: String { 
   let result = String(describing: self)
   let assocValName = Mirror(reflecting: self).children.first?.label ?? "failed"

   return !result.contains("(") ? result : assocValName
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda