gpt4 book ai didi

swift - '(键 : String, 值:任何 )' is not convertible to ' [字符串:任何]'

转载 作者:可可西里 更新时间:2023-11-01 01:50:57 25 4
gpt4 key购买 nike

我正在尝试重构这段代码:

var indices = [String:[Int:Double]]()
apps.forEach { app in indices[app] = [Int:Double]()}

var index = 0
timeSeries.forEach { entry in
entry.apps.forEach{ (arg: (key: String, value: Double)) in
let (app, value) = arg
indices[app]?[index] = value
}

index += 1
}

所以我有签名:

var parameters = timeSeries.map{ entry in entry.apps as [String:Any] }
var indices = getIndices(with: apps, in: parameters) as? [String:[Int:Double]] ?? [String:[Int:Double]]()

和方法:

func getIndices(with source: [String], in entryParameters: [[String:Any]]) -> [String:[Int:Any]] {
var indices = [String:[Int:Any]]()
source.forEach { item in indices[item] = [Int:Any]() }

var index = 0
entryParameters.forEach { (arg: (key: String, value: Any)) in
let (key, value) = arg
indices[key]?[index] = value

index += 1
}

return indices
}

但这(仅在方法中,而不是原始方法中,效果很好)给出:'(key: String, value: Any)' is not convertible to '[String : Any]'entryParameters

我必须使用 Any 的原因是因为另一个来源是 [String:[Int:Bool]]

编辑:更多细节:

时间序列是[TimeSeriesEntry]

// this will need to be defined per-model, so in a different file in final project
struct TimeSeriesEntry: Codable, Equatable {
let id: String
let uid: String
let date: Date
let apps: [String:Double]
let locations: [String:Bool]

func changeApp(app: String, value: Double) -> TimeSeriesEntry {
var apps = self.apps
apps[app] = value

return TimeSeriesEntry(id: self.id, uid: self.uid, date: self.date, apps: apps, locations: self.locations)
}
}

注意事项:

更改调用签名,感谢印象。问题依然存在。

最佳答案

问题是 entryParameters 中的每个值都是一个字典,所以当您执行 entryParameters.forEach 时,您在闭包中得到的字典类型不是 (key, value )

当你在这个字典上调用forEach时,你会得到(key,value)。所以你的方法应该看起来像这样:

func getIndices(with source: [String], in entryParameters: [[String:Any]]) -> [String:[Int:Any]] {
var indices = [String:[Int:Any]]()
source.forEach { item in indices[item] = [Int:Any]() }

var index = 0
entryParameters.forEach { entry in
entry.forEach {(arg: (key: String, value: Any)) in
let (key, value) = arg
indices[key]?[index] = value
}

index += 1
}

return indices
}

关于swift - '(键 : String, 值:任何 )' is not convertible to ' [字符串:任何]',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54236582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com