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

245
Visualizações
Is it possible to combine switch case default and some of the case when they do the same thing

I have a question about switch case in Swift. I have a struct

struct State {
    static let initialized = "init"
    static let ready = "ready"
    static let recording = "recording"
    static let finished = "finished"
    static let error = "error"
}

and I want to return UIImage depending on the current State.

func getHeaderIconImage(from status: String) -> UIImage {
    var result = UIImage()
    switch status {
    case State.ready, State.finished:
        result = Images.ready
    case State.recording:
        result = Images.ongoing
    case State.initialized, State.error:
        result = Images.notReady
    default:
        result = Images.notReady
    }
    return result
}

Basically, the default part should not be triggered, but without the default part Swift will yelling at me with Switch must be exhaustive error, so I need to include the default case. Since I do the exact same thing for the .initialized, .error case, and the default case, probably it's better to combine those three parts and I should only write it down the default case. However, I though if I can also write the .initialized case and the .error case, it's more clear since I can list all the cases in the switch case.

So, I was wonderign if there is a way to refactor the switch state to combine the default case with some other cases if they do exactly the same thing? Or I cannot and either leave the .init & .error case and default, or get rid of the .init & .error part?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You are switching over a String value. The compiler doesn't know you are only using the static properties of your State struct, and technically you are not limited to that. So, in this case the compiler knows that other String values are possible and you need to add a default case. When you use an enum:

enum State: String {
    case initialized = "init"
    case ready = "ready"
    case recording = "recording"
    case finished = "finished"
    case error = "error"
}

The compiler knows all possible case and won't force you to use a default case.

Using the enum, your function should look like this: getHeaderIconImage(from status: State) -> UIImage

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