如何通过带有 FetchedResults 和 CoreData 的导航链接传递数据?
Posted
技术标签:
【中文标题】如何通过带有 FetchedResults 和 CoreData 的导航链接传递数据?【英文标题】:How can I pass data through a Navigation Link with FetchedResults and CoreData? 【发布时间】:2020-12-25 23:34:32 【问题描述】:我正在尝试通过 ScrollView 中的导航链接将 FetchResult 数据从一个屏幕传递到另一个屏幕。
当我尝试在 DetailView 屏幕中调用传递的数据时,我收到以下错误:
Instance member "" cannoth be used on type "SavedPoem"
下面的代码 - 保存的诗歌列表
import SwiftUI
struct SavedPoemList: View
@Environment(\.managedObjectContext) var moc
@FetchRequest(entity: SavedPoem.entity(), sortDescriptors: []) var savedpoems : FetchedResults<SavedPoem>
var body: some View
VStack (alignment: .leading, spacing: 0)
HStack
Text("Your Saved Poems")
.font(.title)
.fontWeight(.black)
.foregroundColor(.black)
Spacer()
.padding(.bottom)
.padding(.trailing)
.padding(.leading)
ScrollView
ForEach(savedpoems, id:\.title) SavedPoem in
NavigationLink (destination: DetailViewSaved())
ZStack
Rectangle()
.fill(Color.white)
.frame(width: UIScreen.screenWidth - 40, height: 70)
.cornerRadius(5)
.padding([.horizontal], 20)
// .shadow(color: .gray, radius: 10)
HStack
VStack (alignment: .leading)
Text("\(SavedPoem.title ?? "")")
.font(.headline)
.foregroundColor(.black)
.lineLimit(1)
.padding(.bottom, 3)
Text("\(SavedPoem.author ?? "")")
.font(.subheadline)
.foregroundColor(.secondary)
.padding(.trailing)
Spacer()
.padding()
.padding(.bottom,10)
.onDelete(perform: self.remove)
.navigationTitle("My Saved Poems")
.navigationBarHidden(true)
.edgesIgnoringSafeArea(.top)
.padding(.bottom, 30)
.padding(.horizontal, 30)
.edgesIgnoringSafeArea(.bottom)
func remove(at offsets : IndexSet)
for index in offsets
let delete = savedpoems[index]
self.moc.delete(delete)
try? self.moc.save()
和详细视图的代码
import SwiftUI
struct DetailViewSaved: View
@ObservedObject var fetch = FetchPoem()
@Environment(\.managedObjectContext) var moc
@FetchRequest(entity: SavedPoem.entity(), sortDescriptors: []) var savedpoems : FetchedResults<SavedPoem>
@State private var saved : Bool = false
var currentDate = Text(Date().addingTimeInterval(600), style: .date)
var body: some View
VStack
HStack
// NavigationLink( destination: HomeView())
//
//
// Image(systemName: "arrow.backward")
// .font(.system(size: 25, weight: .heavy))
// .foregroundColor(.black)
//
Spacer(minLength: 0)
Button(action:
// self.moc.delete(delete)
try? self.moc.save()
)
Image(systemName: saved ? "bookmark.fill": "bookmark")
.font(.system(size: 25, weight: .heavy))
.foregroundColor(.black)
ScrollView
VStack
HStack
VStack (alignment: .leading)
Text("Today's Poem, \(currentDate)")
.font(.subheadline)
.foregroundColor(Color.gray)
.padding(.bottom, 20)
.padding(.top, 10)
Text("\(SavedPoem.title ?? "")")
// .font(.largeTitle)
// .fontWeight(.heavy)
// .foregroundColor(.black)
// .padding(.bottom, 20)
// .lineSpacing(0)
// Text("BY "+poem.author.uppercased())
Text("BY \(SavedPoem.author ?? "")")
// .font(.subheadline)
// .foregroundColor(Color.gray)
// .padding(.bottom, 20)
HStack
Text("\(SavedPoem.lines ?? "")")
// .font(.body)
// .foregroundColor(.black)
// .padding(.bottom)
// .lineSpacing(5)
//
Spacer()
Spacer()
.padding()
我错过了什么吗?如何解决此问题,并将列表视图中 Fetchedresults 中的数据传递到详细屏幕?
提前致谢。
编辑后的回复代码
已保存的诗歌列表:
import SwiftUI
struct SavedPoemList: View
@Environment(\.managedObjectContext) var moc
@FetchRequest(entity: SavedPoem.entity(), sortDescriptors: []) var savedpoems : FetchedResults<SavedPoem>
var body: some View
VStack (alignment: .leading, spacing: 0)
HStack
Text("Your Saved Poems")
.font(.title)
.fontWeight(.black)
.foregroundColor(.black)
Spacer()
Text("Edit")
.font(.subheadline)
.fontWeight(.black)
.foregroundColor(.gray)
.padding(.bottom)
.padding(.trailing)
.padding(.leading)
ScrollView
ForEach(savedpoems, id:\.title) SavedPoem in
NavigationLink (destination: DetailViewSaved(savedPoem: SavedPoem))
ZStack
Rectangle()
.fill(Color.white)
.frame(width: UIScreen.screenWidth - 40, height: 70)
.cornerRadius(5)
.padding([.horizontal], 20)
// .shadow(color: .gray, radius: 10)
HStack
VStack (alignment: .leading)
Text("\(SavedPoem.title ?? "")")
.font(.headline)
.foregroundColor(.black)
.lineLimit(1)
.padding(.bottom, 3)
Text("\(SavedPoem.author ?? "")")
.font(.subheadline)
.foregroundColor(.secondary)
.padding(.trailing)
Spacer()
.padding()
.padding(.bottom,10)
.onDelete(perform: self.remove)
.navigationTitle("My Saved Poems")
.navigationBarHidden(true)
.edgesIgnoringSafeArea(.top)
.padding(.bottom, 30)
.padding(.horizontal, 30)
.edgesIgnoringSafeArea(.bottom)
func remove(at offsets : IndexSet)
for index in offsets
let delete = savedpoems[index]
self.moc.delete(delete)
try? self.moc.save()
详细视图
import SwiftUI
struct DetailViewSaved: View
@ObservedObject var fetch = FetchPoem()
@Environment(\.managedObjectContext) var moc
@FetchRequest(entity: SavedPoem.entity(), sortDescriptors: []) var savedpoems : FetchedResults<SavedPoem>
@State private var saved : Bool = false
var savedPoem : SavedPoem
var currentDate = Text(Date().addingTimeInterval(600), style: .date)
var body: some View
VStack
HStack
// NavigationLink( destination: HomeView())
//
//
// Image(systemName: "arrow.backward")
// .font(.system(size: 25, weight: .heavy))
// .foregroundColor(.black)
//
Spacer(minLength: 0)
Button(action:
// self.moc.delete(delete)
try? self.moc.save()
)
Image(systemName: saved ? "bookmark.fill": "bookmark")
.font(.system(size: 25, weight: .heavy))
.foregroundColor(.black)
ScrollView
VStack
HStack
VStack (alignment: .leading)
// Text("Today's Poem, \(currentDate)")
// .font(.subheadline)
// .foregroundColor(Color.gray)
// .padding(.bottom, 20)
// .padding(.top, 10)
Text("\(savedPoem.title ?? "")")
// .font(.largeTitle)
// .fontWeight(.heavy)
// .foregroundColor(.black)
// .padding(.bottom, 20)
// .lineSpacing(0)
// Text("BY "+poem.author.uppercased())
// Text("BY \(SavedPoem.author ?? "")")
// .font(.subheadline)
// .foregroundColor(Color.gray)
// .padding(.bottom, 20)
// HStack
// Text("\(SavedPoem.lines ?? "")")
// .font(.body)
// .foregroundColor(.black)
// .padding(.bottom)
// .lineSpacing(5)
//
Spacer()
Spacer()
.padding()
struct DetailViewSaved_Previews: PreviewProvider
static var previews: some View
DetailViewSaved(savedPoem: SavedPoem)
【问题讨论】:
【参考方案1】:我用你的例子做了一个例子:
SavedPoemList:像这样将数组中的项目传递给Detail View
:
NavigationLink (destination: DetailViewSaved(savedPoem: SavedPoem))
VStack
Text("Your row view")
并通过您的Detail View
接收它:
struct DetailViewSaved: View
var savedPoem : SavedPoem
var body: some View
Text("Your view")
【讨论】:
谢谢,Ouail。我尝试了您的建议并得到以下错误: - 在 SavedPoemList 中,在 ForEach:“无法推断通用参数“内容”” - 在详细视图中,声明变量时:“类型名称后预期的成员名称调用 - 详细查看,调用变量时:Text("(savedPoem.title ?? "")"), "Instance member "title" cannot be used on type "Save Poem" @Simbeul 你能用你得到的错误编辑你的问题吗 谢谢!我在上面的评论中列出了错误。 编辑:我打错了,用 var savedPoem = SavedPoem 而不是 var savedPoem : SavedPoem...我的错! 还有一个错误:在我的 struct DetailViewSaved_Previews 中,它抛出:Missing Argument for parameter "SavedPoem" in call... 我应该添加什么?以上是关于如何通过带有 FetchedResults 和 CoreData 的导航链接传递数据?的主要内容,如果未能解决你的问题,请参考以下文章
SwiftUI @FetchedResults 一对多关系没有正确更新
如何在 SwiftUI 中从 FetchedResults (CoreData) 中获取 NSOrderedSet
Swift & Core Data 解包 fetchedresults
tableView endUpdates 在 fetchedResults 更改后崩溃
SwiftUI 核心数据错误 - “无法将 'FetchedResults<GCItem>' 类型的值转换为预期的参数类型 'Range<Int>'”