作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这样的看法:
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)")
}
}
}
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 Hashable
在
isSectionView: 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/
我是一名优秀的程序员,十分优秀!