Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

153
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda