javascript - 无法分配类型的值(又名“数组”>') 到 (又名'字典')
问题描述
这是我的代码:
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
}
注意:var stylesMap: [[StyleID: Style]]?
如何创建字典stylesArray
?(我认为 javascript 中的 Map 是 swift 中的字典)
我正在尝试将javascript代码移植到swift。下面是javascript代码:
static style(
node: Node,
baseProps: BaseConstructorProperties,
): this {
const stylesArray = assertNonNull(
Node?.stylesMap,
'stylesMap is missing',
);
return new this({
...baseProps,
stylesMap: new Map(stylesArray),
});
}
解决方案
如果您只想要第一项,如评论中所述,您可以替换以下行:
objectNode.stylesMap = stylesArray
和:
guard let first = stylesArray.first else {
throw UEErrors.unrecoverableError(message: "No items")
}
objectNode.stylesMap = first
推荐阅读
- android - kapt 发生异常:java.lang.OutOfMemoryError: GC 开销限制超出
- c# - 在 Xamarin 中获取用户安装的应用程序列表
- c# - 具有一个字段(即整数)的结构 - 您还需要覆盖 GetHashCode、Equals 等吗?
- c++ - 在 Visual Studio 2017 中创建的简单 DLL 无法在 XP 中加载
- swift - 执行 segue 时的异步检查
- regex - 正则表达式从所有场景的 URL 中剥离域?
- typescript - 从我的 Cordova 自定义插件创建 Ionic TypeScript 包装器
- node.js - 如何使用 expressjs 在 Firebase 中加入两个集合?
- corda - Corda 中公证变更交易的语法
- arrays - 如何在反应原生数组中获取所选元素的索引