【发布时间】:2022-01-12 08:52:51
【问题描述】:
我有以下 5 个字典
var serialsDict = [ Source(address: 0, endpoint: 3) : "860408ff",
Source(address: 0, endpoint: 4) : "560410ff"]
var namesDict = [ Source(address: 0, endpoint: 3) : "SLIGHTR10",
Source(address: 0, endpoint: 4) : "SLIGHTR09"]
var fwVersionsDict = [ Source(address: 0, endpoint: 3) : "FG01b",
Source(address: 0, endpoint: 4) : "FG02c"]
var hwVersionDict = [ Source(address: 0, endpoint: 3) : "604a1r00",
Source(address: 0, endpoint: 4) : "674b1r45"]
var typesDict = [ Source(address: 0, endpoint: 3) : 4,
Source(address: 0, endpoint: 4) : 1]
Source 在哪里
struct Source: Hashable {
let address: Int
let endpoint: Int
}
我必须合并 5 个字典以获得包含 2 个 FoundDevice 对象的数组。其中FoundDevice 是以下结构:
struct FoundDevice {
let source: Source
let serial: String
let name: String
let fwVersion: String
let hwVersion: String
let type: Int
}
例如第一个元素必须是
FoundDevice(source: Source(address: 0, endpoint: 3), serial: “860408ff”,名称:“SLIGHTR10”,fwVersion:“FG01b”,hwVersion: “604a1r00”,类型:4)
这是我当前的实现:
serialsDict.keys.forEach { source in
if let serial = serialsDict[source], let name = namesDict[source], let fwVersion = fwVersionsDict[source], let hwVersion = hwVersionDict[source], let type = typesDict[source] {
devices.append(FoundDevice(source: source, serial: serial, name: name, fwVersion: fwVersion, hwVersion: hwVersion, type: type))
}
}
有没有更聪明的方法来实现我的目标?
【问题讨论】:
标签: swift dictionary