【发布时间】:2020-07-15 20:47:34
【问题描述】:
我有这样的看法:
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 in isSectionView: self.$isSectionViewArray[dateStr, default: true]
为什么isSectionViewArray:[String:Bool] = [:] 不是 Hasbable?
如何修改工作代码?
如果我在SectionView、@Binding var isSectionView:Bool 中删除,代码可以正常工作,或者如果我从SectionView、@Binding var isSectionViewArray:[String:Bool] = [:] 中设置,代码可以正常工作。
【问题讨论】:
标签: dictionary swiftui hashable