SwiftUI和CoreData:如何计算“ true”布尔值的数量并在VGrid中显示结果

问题描述

我创建了一个名为Event的CoreData实体,其属性为category(字符串)和isBlack(布尔)。我还创建了一个Button&VGrid(Xcode 12 beta)。

单击该按钮后,会将一些条目添加并保存到CoreData。我有一个@FetchRequest和一个函数update,该函数返回一个字典,该字典将CoreData条目分组在各自的类别中。 然后,我让VGrid发挥神奇的作用,并使用SFSymbols图像在每个类别中显示一个部分。

我想要实现的是VGrid显示一个Image,具体取决于每个类别中有多少个“ true”布尔值。因此,例如,如果真正的布尔值在某个类别中占大多数,我想显示circle.filled,否则只显示circle

以下是代码,因为它使用了Grid,因此仅在Xcode 12(测试版)中有效:

struct ContentView: View {
    
    @Environment(\.managedObjectContext) var moc
    @FetchRequest(entity: Event.entity(),sortDescriptors: []) var events: FetchedResults<Event>
    
    // grouping CoreData entries by categories,returning Dictionary:
    func update(_ result : FetchedResults<Event>)-> [[Event]]{
        return Dictionary(grouping: result){ (element : Event)  in
            element.category!
        }.values.map{$0}
    }
    
    
    let columns = [
        GridItem(.flexible()),GridItem(.flexible()),GridItem(.flexible())
    ]
    
    var body: some View {
        VStack {
            Button(action: {
                
                // adding some events in 5 categories,in total 5 grid colums.
                
                let newEvent1 = Event(context: self.moc)
                newEvent1.category = "A"
                newEvent1.isBlack = true
                
                let newEvent2 = Event(context: self.moc)
                newEvent2.category = "A"
                newEvent2.isBlack = true
                
                let newEvent3 = Event(context: self.moc)
                newEvent3.category = "A"
                newEvent3.isBlack = false
                
                let newEvent4 = Event(context: self.moc)
                newEvent4.category = "B"
                newEvent4.isBlack = false
                
                let newEvent5 = Event(context: self.moc)
                newEvent5.category = "C"
                newEvent5.isBlack = true
                
                let newEvent6 = Event(context: self.moc)
                newEvent6.category = "C"
                newEvent6.isBlack = false
                
                let newEvent7 = Event(context: self.moc)
                newEvent7.category = "D"
                newEvent7.isBlack = true
                
                let newEvent8 = Event(context: self.moc)
                newEvent8.category = "D"
                newEvent8.isBlack = true
                
                let newEvent9 = Event(context: self.moc)
                newEvent9.category = "D"
                newEvent9.isBlack = false
                
                let newEvent10 = Event(context: self.moc)
                newEvent10.category = "E"
                newEvent10.isBlack = true
                
                
                try? self.moc.save()
                
            }) {
                Text("Add some Events")
            }
            
            // Grid:
            
            LazyVGrid(columns: columns,spacing: 40) {
                ForEach(update(events),id: \.self) { (section: [Event]) in
                    NavigationLink(destination: Text("this just a test"))
                    {
                        
                        // here comes the part i need advice...
                        // how would i sum up all the true booleans and check if they're the majority in each category?
                        
                        if section[0].isBlack == true {
                            Image(systemName: "circle.fill")
                        } else {
                            Image(systemName: "circle")
                        }
                        
                        // so what i want to achieve is following images being displayed:
                        // first column (catergory A): circle.filled (cause true booleans are more)
                        // second column(catergory B): circle
                        // third column(catergory C): circle.filled (if equal => same as if true)
                        // fourth column(catergory D): circle
                        // fifth Column(catergory E): circle.filled
                        
                    }
                }
            }
            
        }
    }
}

非常感谢任何帮助,非常感谢。

解决方法

所以今天,我想出了使它工作的方法,尽管我不得不将isBlack?布尔值更改为String,然后像这样减少分组的部分:

let whiteCount = section.reduce(0) { $0 + (($1 as AnyObject).isBlack!.contains("false") ? 1 : 0) }
let blackCount = section.reduce(0) { $0 + (($1 as AnyObject).isBlack!.contains("true") ? 1 : 0) }

现在,只需比较whiteCountblackCount的值并在网格中显示相应的图像即可。

尽管如此,我还是更喜欢使用布尔值的解决方案。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...