【问题标题】:SWIFTUI Call Key Dictionary not work with the error: 'Subscript index of type '() -> Bool' in a key path must be Hashable'SWIFTUI 调用密钥字典不适用于错误:“密钥路径中的“() -> Bool”类型的下标索引必须是可散列的”
【发布时间】: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


    【解决方案1】:

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

    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
                }
            }
        }
    

    【讨论】:

    • 使用您的解决方案,我在ForEach( 中出现此错误:Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols。感谢您的建议,我找到了这个post,但即使这样也没有帮助我。您有其他解决此问题的想法吗?
    • @Cesare Piersigilli 我认为您以某种方式更改了代码并且发生了此错误。我尝试了上面建议的代码,它工作正常 - 没有错误。
    猜你喜欢
    • 1970-01-01
    • 2018-12-04
    • 1970-01-01
    • 2020-03-18
    • 2016-02-17
    • 2020-02-01
    • 2011-12-04
    • 2019-09-30
    • 1970-01-01
    相关资源
    最近更新 更多