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



我创建了一个名为Event的CoreData实体,其属性为category(String(和isBlack(Bool(。我还创建了一个按钮&VGrid(Xcode 12 beta(。

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

我想要实现的是VGrid根据多少";真";布尔型在每个类别中。因此,例如,如果真正的布尔值在一个类别中占大多数,我想显示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()),
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?布尔值更改为字符串,然后减少分组部分,如下所示:

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

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

最新更新