2

我创建了一个 CoreData 实体 Event,用它的属性category(String) 和isBlack(Bool) 调用。我还创建了一个 Button 和一个 VGrid(Xcode 12 beta)。

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

我想要实现的是 VGrid 根据每个类别中有多少“真实”布尔值显示一个图像。因此,例如,如果真正的布尔值在一个类别中占多数,我想显示circle.filled,否则只是circle

这是代码,因为它使用了 Grid,所以它只适用于 Xcode 12 (beta):

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()),
        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
                        
                    }
                }
            }
            
        }
    }
}

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

4

1 回答 1

0

所以今天,我想出了如何让它工作,虽然我不得不将isBlack?布尔值更改为字符串,然后像这样减少分组部分:

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在网格中显示相应的图像。

不过,我仍然更喜欢使用布尔值的解决方案。

于 2020-08-17T20:02:56.963 回答