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

150
Visualizações
Initializer for conditional binding must have Optional type, not '[Key : Value?]'

I'm trying to translate javascript to swift. This is the javascript method:

export function serializeProperty(
      properties: Map<Key, Value | null>,
    ): Array<[Key, JsonValue | null]> {
      const data = Array.from(properties.entries());
      const processedData = data.map(entry => {
        const [key, value] = entry;
        if (value != null) {
          return [key, serializeValue(value)];
        }
        return [key, null];
      });
      return processedData;
    }
export function serializeValue(Value: Value): JsonValue {
  if (Value.type === 'string') {
    return {type: 'string', value: Value.value.toJson()};
  } else if (Value.type === 'array_node') {
    return {
      type: 'array_node',
      value: Value.value.map(node => node.toJson()),
    };
  }
  return Value;
}

I have translated it in swift like below:

func serializeProperty(properties: [Key: Value?]) -> [[Key: JsonValue?]]? {

  var data: [[Key: JsonValue?]]?

  guard let propertiesJson = properties else { return data }
  for property in propertiesJson {
    for item in property {
      if let unwrappedValue = item.value {
        properties[item.key] = try serializeValue(valueJson: unwrappedValue)
      }
    }
  }

  return data
}

I'm getting the error on this line : guard let propertiesJson = properties else { return data } Is the translation to swift correct or am I missing something?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The simplest translation of this TypeScript to Swift will look something like:

func serializeProperty(properties: Dictionary<Key, Value?>) -> Array<(Key, JsonValue?)> {
  let processedData = properties.map() { key, value -> (Key, JsonValue?) in
    if let value = value {
      return (key, serializeValue(value: value))
    }   

    return (key, nil)
  }
  return processedData
}

Note the use of (Key, JsonValue?) in regular parentheses to denote a tuple of Key and JsonValue?, matching the TypeScript notation of [Key, JsonValue | null]. Also note that in both the TypeScript and Swift implementations, the properties parameter is a non-nullable type, so there is no need to check against null/nil.

Using a guard clause and returning the result of properties.map() directly, this can be further simplified to:

func serializeProperty(properties: Dictionary<Key, Value?>) -> Array<(Key, JsonValue?)> {
  return properties.map() { key, value in
    guard let value = value else { return (key, nil) }

    return (key, serializeValue(value: value))
  }
}

Going back the other way, the TypeScript implementation can also be simplified in a similar way to:

function serializeProperty(properties: Map<Key, Value|null>): Array<[Key, JsonValue|null]> {
  return Array.from(properties.entries()).map(([key, value]) => {
    if (value === null) { return [key, null]; }

    return [key, serializeValue(value)];
  }); 
}
about 4 years ago · Juan Pablo Isaza 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