Home > Mobile >  Cannot assign value of type (aka 'Array<Dictionary<String, Style>>') to (aka &
Cannot assign value of type (aka 'Array<Dictionary<String, Style>>') to (aka &

Time:11-12

This is my code:

override func style(node: Node) throws -> StyleNode {
    guard let objectNode = try super.style(node: node) as? StyleNode
    else {
      throw UEErrors.unrecoverableError(message: "Could not create Node")
    }

    guard let stylesArray = node.stylesMap else {
      throw UEErrors.unrecoverableError(message: "stylesMap is missing")
    }
    objectNode.stylesMap = stylesArray //error : Cannot assign value of type '[[StyleID : Style]]' (aka 'Array<Dictionary<String, Style>>') to type '[StyleID : Style]' (aka 'Dictionary<String, Style>')
    return objectNode
  }

Note: var stylesMap: [[StyleID: Style]]? How do I create a dictionary of stylesArray? (I think Map in javascript is dictionary in swift)

I'm, trying to port javascript code to swift. Below is javascript code:

static style(
    node: Node,
    baseProps: BaseConstructorProperties,
  ): this {
    const stylesArray = assertNonNull(
      Node?.stylesMap,
      'stylesMap is missing',
    );

    return new this({
      ...baseProps,
      stylesMap: new Map(stylesArray),
    });
  }

CodePudding user response:

If you want just the first item, as mentioned in the comments, you can replace the following line:

objectNode.stylesMap = stylesArray

with:

guard let first = stylesArray.first else {
  throw UEErrors.unrecoverableError(message: "No items")
}
objectNode.stylesMap = first
  • Related