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

343
Visualizações
Swift errors using #if, #endif

Using #if, #endif in Swift (using Xcode) produces errors if it cuts into the flow of an operation. This screenshot says it all:

Swift #if, #endif error

Does anyone know a solution to make this example work, without repeating the entire code block twice? There can easily be situations where the entire block can be very large.

EDIT: My sample was a bit too simple. Here is a new sample where the "else if" depends on the same define (DEBUG). The "else if" must also be within the #if and #endif. And other samples can be much more complex than this.

enter image description here

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

0

Ideally, limit the usage of #if as much as possible. Using preprocessor directives is always a bit of a code smell. In this case, you can simply use a boolean variable:

#if DEBUG
let debug = true
#else
let debug = false
#endif

Then simply use the variable:

var a = 0
var b = 0

...

else if debug && a == b {
}

In release mode the code will become unreachable and the optimizer will remove it anyway.

With a bit of imagination, we can find other solutions, for example, we can move the check to a function:

func isDebugCheck(a: Int, b: Int) -> Bool {
   #if DEBUG
      return a == b
   #else
      return false
   #endif
}

or we can move the whole code to a separate function and replace if-else by a return (or continue, depending on you needs), e.g.:

if a == 7 {
  ...
  return
}

#if DEBUG
  if a == b {
     return
  }
#endif

if ...
over 4 years ago · Santiago Trujillo Relatório

0

As @user28434 notes, there is no source-level pre-processor. This has gotten rid of a lot of very tricky pre-processor problems in C (such as bizarre needs for parentheses to make things work).

However, #if is integrated well into the language, and specifically supports switch for exactly these kinds of cases.

var a = 0

#if DEBUG
let b = 0
#endif

switch a {
case 7: a += 1
    #if DEBUG
case b: a += 2
    #endif
case 5: a += 3
default:
    break
}
over 4 years ago · Santiago Trujillo Relatório

0

You can simply achieve this case with below code:

if a == b {
    #if DEBUG
    a += 2
    #else
    a += 1
    #endif
} else if a == c {
    a += 3
}
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