使用核心数据存储一些机场,对于每个机场,我存储的是不同的注释
我创建了实体机场和实体简报会
机场有一个属性叫做icaoAPT,简报有四个属性类别,描述,icaoAPT,noteID
在我的detailsView中,我显示了所有与该机场相关的列表,我设法通过另一个名为FilterList的视图进行动态获取。
import SwiftUI
import CoreData
struct FilterLIst: View {
var fetchRequest: FetchRequest<Briefing>
@Environment(\.managedObjectContext) var dbContext
init(filter: String) {
fetchRequest = FetchRequest<Briefing>(entity: Briefing.entity(), sortDescriptors: [], predicate: NSPredicate(format: "airportRel.icaoAPT == %@", filter))
}
func update(_ result : FetchedResults<Briefing>) ->[[Briefing]]{
return Dictionary(grouping: result) { (sequence : Briefing) in
sequence.category
}.values.map{$0}
}
var body: some View {
List{
ForEach(update(self.fetchRequest.wrappedValue), id: \.self) { (section : Briefing) in
Section(header: Text(section.category!)) {
ForEach(section, id: \.self) { note in
Text("hello")
/// Xcode error Cannot convert value of type 'Text' to closure result type '_'
}
}
}
}
}
}在这个视图中,我尝试使用func更新显示按类别划分的所有部分.但是Xcode给了我这个错误,我无法理解why..Cannot将'Text‘类型的值转换为闭包结果类型'_’
前参考我在我的detailsView下面列出
import SwiftUI
struct DeatailsView: View {
@Environment(\.managedObjectContext) var dbContext
@Environment(\.presentationMode) var presentation
@State var airport : Airport
@State var note = ""
@State var noteTitle = ["SAFTY NOTE", "TAXI NOTE", "CPNOTE"]
@State var notaTitleSelected : Int = 0
@State var notaID = ""
var body: some View {
Form{
Section(header: Text("ADD NOTE Section")) {
TextField("notaID", text: self.$notaID)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding()
TextField("add Note descrip", text: self.$note)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding()
Picker(selection: $notaTitleSelected, label: Text("Class of Note")) {
ForEach(0 ..< noteTitle.count) {
Text(self.noteTitle[$0])
}
}
HStack{
Spacer()
Button(action: {
let nota = Briefing(context: self.dbContext)
nota.airportRel = self.airport
nota.icaoAPT = self.airport.icaoAPT
nota.descript = self.note
nota.category = self.noteTitle[self.notaTitleSelected]
nota.noteID = self.notaID
do {
try self.dbContext.save()
debugPrint("salvato notazione")
} catch {
print("errore nel salva")
}
}) {
Text("Salva NOTA")
}
Spacer()
}
}
Section(header: Text("View Note")) {
FilterLIst(filter: airport.icaoAPT ?? "NA")
}
}
}
}谢谢你的帮助
发布于 2020-07-04 09:53:54
这是因为您试图在单个Briefing对象上迭代,而ForEach循环需要一个集合。
List {
ForEach(update(self.fetchRequest.wrappedValue), id: \.self) { (section: Briefing) in
Section(header: Text(section.category!)) {
ForEach(section, id: \.self) { note in // <- section is a single object
Text("hello")
/// Xcode error Cannot convert value of type 'Text' to closure result type '_'
}
}
}
}为了清晰起见,我建议您将第二个ForEach提取到另一个方法中。通过这种方式,您还可以确保传递正确类型([Briefing])的参数:
func categoryView(section: [Briefing]) -> some View {
ForEach(section, id: \.self) { briefing in
Text("hello")
}
}注意,update方法的结果是[[Briefing]]类型,这意味着ForEach中的参数是section: [Briefing] (而不是Briefing):
var body: some View {
let data: [[Briefing]] = update(self.fetchRequest.wrappedValue)
return List {
ForEach(data, id: \.self) { (section: [Briefing]) in
Section(header: Text("")) { // <- can't be `section.category!`
self.categoryView(section: section)
}
}
}
}这也意味着您不能在头中写入section.category!,因为section是一个数组。
您可能需要访问Briefing对象才能获得类别:
Text(section[0].category!)(如果您确定第一个元素存在)。
为了清晰起见,我显式地指定了类型。这也是一个很好的方式来确保你总是使用正确的类型。
let data: [[Briefing]] = update(self.fetchRequest.wrappedValue)然而,Swift可以自动推断类型。在下面的示例中,data将是[[Briefing]]类型。
let data = update(self.fetchRequest.wrappedValue)https://stackoverflow.com/questions/62727370
复制相似问题