我有以下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", name: "SLIGHTR10", fwVersion: "FG01b", hwVersion: "604a1r00", type: 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))
}
}
有沒有更聰明的方法來實作我的目標?
uj5u.com熱心網友回復:
沒有可用的“技巧”,但您不需要再次檢查其中一個字典中的值;你已經有了。
extension FoundDevice {
init?(
source: Source,
serial: String,
names: [Source: String],
fwVersions: [Source: String],
hwVersions: [Source: String],
types: [Source: Int]
) {
guard
let name = names[source],
let fwVersion = fwVersions[source],
let hwVersion = hwVersions[source],
let type = types[source]
else { return nil }
self.init(
source: source,
serial: serial,
name: name,
fwVersion: fwVersion,
hwVersion: hwVersion,
type: type
)
}
}
serialsDict.compactMap {
FoundDevice(
source: $0.key,
serial: $0.value,
names: namesDict,
fwVersions: fwVersionsDict,
hwVersions: hwVersionDict,
types: typesDict
)
}
在下面解決您的評論,
我的字典可能包含比其他字典更多的鍵。僅當鍵在每個字典中時,我才必須創建我的物件。
無論您使用哪個字典進行迭代,上面的 guard 陳述句都會處理這個問題。
但是,如果您周圍有鑰匙的交集,無論出于何種原因,這就是……
let sources = [serialsDict, namesDict, fwVersionsDict, hwVersionDict]
.reduce(into: Set(typesDict.keys)) {
$0.formIntersection($1.keys)
}
...然后您可以安全地強制展開。
extension FoundDevice {
/// - Precondition: `source` is a key in all of the dictionaries.
init(
source: Source,
serials: [Source: String],
names: [Source: String],
fwVersions: [Source: String],
hwVersions: [Source: String],
types: [Source: Int]
) {
self.init(
source: source,
serial: serials[source]!,
name: names[source]!,
fwVersion: fwVersions[source]!,
hwVersion: hwVersions[source]!,
type: types[source]!
)
}
}
sources.map {
FoundDevice(
source: $0,
serials: serialsDict,
names: namesDict,
fwVersions: fwVersionsDict,
hwVersions: hwVersionDict,
types: typesDict
)
}
uj5u.com熱心網友回復:
您需要將一組資料映射到另一組型別。那將是地圖。但是由于您使用字典,因此我們在這里需要緊湊的地圖。
let sources = serialsDict.map({ $0.key })
let foundDevices: [FoundDevice] = sources.compactMap({
if let serial = serialsDict[$0],
let name = namesDict[$0],
let fwVersion = fwVersionsDict[$0],
let hwVersion = hwVersionDict[$0],
let typ = typesDict[$0] {
return FoundDevice(source: $0, serial: serial, name: name, fwVersion: fwVersion, hwVersion: hwVersion, type: typ)
}
return nil
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/375704.html
上一篇:如何遍歷矩陣并更改其值?
