gpt4 book ai didi

dictionary - SWIFTUI 调用 key 字典无法使用错误 : 'Subscript index of type ' () -> Bool' in a key path must be Hashable'

转载 作者:行者123 更新时间:2023-12-04 12:59:16 32 4
gpt4 key购买 nike

我有这样的看法:

import SwiftUI

struct SectionView1: View {

let dateStr:String
@Binding var isSectionView:Bool

var body: some View {
HStack {
Button(action: {
self.isSectionView.toggle()
}) {
Image(systemName: isSectionView ? "chevron.down.circle" : "chevron.right.circle")
}
Text("Media del \(dateStr)")
}
}
}

将从 View 中调用:
import SwiftUI
import Photos

struct MediaView: View {
let geoFolder:GeoFolderCD

@State private var assetsForDate = [String :[PHAsset]]()
@State private var isSectionViewArray:[String:Bool] = [:]

var body: some View {
List {
ForEach(assetsForDate.keys.sorted(by: > ), id: \.self) { dateStr in
Section {
SectionView1(dateStr: dateStr,
isSectionView: self.$isSectionViewArray[dateStr, default: true])
}
}
}
.onAppear {
self.assetsForDate = FetchMediaUtility().fetchGeoFolderAssetsForDate(geoFolder: geoFolderStruct, numAssets: numMediaToFetch)
for dateStr in self.assetsForDate.keys.sorted() {
self.isSectionViewArray[dateStr] = true
}
}
}
}

但我有错误: Subscript index of type '() -> Bool' in a key path must be HashableisSectionView: self.$isSectionViewArray[dateStr, default: true]
为什么 isSectionViewArray:[String:Bool] = [:]是不是 Hasbable?

如何修改工作代码?

如果我删除,在 SectionView , @Binding var isSectionView:Bool ,代码工作正常,或者如果我设置,来自 SectionView , @Binding var isSectionViewArray:[String:Bool] = [:] ,代码工作正常。

最佳答案

您可以使用以下代码编写自己的绑定(bind),它应该可以工作

var body: some View {
List {
ForEach(assetsForDate.keys.sorted(by: > ), id: \.self) { dateStr in
let value = Binding<Bool>(get: { () -> Bool in
return self.isSectionViewArray[dateStr, default: true]
}) { (value) in

}
Section {
SectionView1(dateStr: dateStr,
isSectionView: value)
}
}
}
.onAppear {
self.assetsForDate = FetchMediaUtility().fetchGeoFolderAssetsForDate(geoFolder: geoFolderStruct, numAssets: numMediaToFetch)
for dateStr in self.assetsForDate.keys.sorted() {
self.isSectionViewArray[dateStr] = true
}
}
}

关于dictionary - SWIFTUI 调用 key 字典无法使用错误 : 'Subscript index of type ' () -> Bool' in a key path must be Hashable',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61016009/

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