Dam*_*zzi 5 core-data swift swiftui
使用核心数据我存储一些机场和每个机场我存储不同的笔记
我创建了实体机场和实体简报
Airport 有 1 个属性称为 caoAPT,而 Briefing 有 4 个属性 category、descript、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 '_'
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
在此视图中,我尝试使用 func 更新显示按类别划分的所有部分...但 Xcode 给我这个错误,我不明白为什么..无法将“文本”类型的值转换为关闭结果类型“ _'
前参考我在我的详细信息下面列出
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")
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢您的帮助
这是因为您尝试迭代单个 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 '_'
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
为了清楚起见,我建议您将第二个方法提取ForEach到另一种方法。这样您还可以确保传递正确类型的参数 ( [Briefing]):
func categoryView(section: [Briefing]) -> some View {
ForEach(section, id: \.self) { briefing in
Text("hello")
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,您的方法的结果update是类型,这意味着is (而不是)[[Briefing]]中的参数:ForEachsection: [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)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这也意味着您不能section.category!在标头中写入,因为它section是一个数组。
您可能需要访问一个Briefing对象来获取类别:
Text(section[0].category!)
Run Code Online (Sandbox Code Playgroud)
(如果您确定第一个元素存在)。
为了清楚起见,我明确指定了类型。这也是确保您始终使用正确类型的好方法。
let data: [[Briefing]] = update(self.fetchRequest.wrappedValue)
Run Code Online (Sandbox Code Playgroud)
然而,Swift 可以自动推断类型。在下面的示例中,遗嘱data的类型为[[Briefing]]:
let data = update(self.fetchRequest.wrappedValue)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
557 次 |
| 最近记录: |