我正在尝试将javascript翻译成swift。这是javascript方法:
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;
}
我已经快速翻译了它,如下所示:
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
}
我在这一行遇到错误:guard let propertiesJson = properties else { return data }
swift 的翻译是正确的还是我遗漏了什么?