[英]How can I use relationship in ForEach in CoreData using SwiftUI?
The problem has been solved by 'hackingwithswift' website 'hackingwithswift' 网站已经解决了这个问题
Just follow the steps on the link: "https.//www.hackingwithswift.com/books/ios-swiftui/one-to-many-relationships-with-core-data-swiftui-and-fetchrequest"只需按照链接上的步骤操作:“https.//www.hackingwithswift.com/books/ios-swiftui/one-to-many-relationships-with-core-data-swiftui-and-fetchrequest”
original ask原问
I would like to use elements of [relationship]我想使用 [relationship] 的元素
But whatever I try, it doesn't work..但无论我尝试什么,它都不起作用..
There is something, what I missed and, sadly I don't know what that would be until now:/有一些东西,我错过了,遗憾的是我直到现在才知道那会是什么:/
First of all, I want to show my core data model and code.首先,我想展示我的核心数据 model 和代码。
Core Data Model Image核心数据 Model 图像
Entity: Page, Attribute: name(String), relationship: toCard实体:页面,属性:名称(字符串),关系:toCard
Entity: Card, Attribute: title(String), price(Double), relationship: toPage实体:卡片,属性:title(String),price(Double),关系:toPage
Scenario: Page has multiple cards场景:页面有多张卡片
One to Many (One page to many cards)一对多(一页对多张卡片)
ContentView.swift
TabView(selection: self.$pageIndex){
MainPageView()
ForEach(pages, id: \.self) { page in
SubPageView(whichPage: page)
}
}
SubPageView.swift
@Environment(\.managedObjectContext) var moc
@FetchRequest(entity: Page.entity(),
sortDescriptors: [NSSortDescriptor(keyPath: \Page.name, ascending: true),
NSSortDescriptor(keyPath: \Page.toCard, ascending: true)]
) var pages: FetchedResults<Page>
@FetchRequest(entity: Card.entity(),
sortDescriptors: [NSSortDescriptor(keyPath: \Card.title, ascending: true),
NSSortDescriptor(keyPath: \Card.price, ascending: true)]
) var cards: FetchedResults<Card>
var whichPage: FetchedResults<Page>.Element
ForEach(pages, id: \.self) { page in // page: 1번 2번 3번
if whichPage == page {
ForEach(Array(whichPage.toCard! as Set), id: \.self) { relationshipFromToCard in
}
}
The Problem is here问题就在这里
ForEach(Array(whichPage.toCard! as Set), id: \.self) { item in
CardView(price: item.price, title: item.title ?? "Unknown")
}
I can not use like this in CardView.我不能在 CardView 中这样使用。
Xcode doesn't show the Attributes of Card, because they are not matching. Xcode 不显示卡的属性,因为它们不匹配。
How can I fix it..?我该如何解决它..?
The data of Card has been saved in relationship (code below) Card的数据已经保存在关系中(代码如下)
let card = Card(context: self.moc)
card.title = self.title
card.price = self.doubleValue
selectedPage?.addToToCard(card)
I solved this problem with help from this awesome 'hackingwithswift' website!我在这个很棒的“hackingwithswift”网站的帮助下解决了这个问题! Just follow the steps on the link!
只需按照链接上的步骤操作!
https://www.hackingwithswift.com/books/ios-swiftui/one-to-many-relationships-with-core-data-swiftui-and-fetchrequest https://www.hackingwithswift.com/books/ios-swiftui/one-to-many-relationships-with-core-data-swiftui-and-fetchrequest
Edited:编辑:
Goto.xcdatamodeld file Goto.xcdatamodeld 文件
Click Entity -> Class -> Codezen to Manual/None单击实体 -> Class -> Codezen 到手动/无
Editor -> Create NSManagedObject Subclass编辑器 -> 创建 NSManagedObject 子类
Add in Page+CoreDataProperties.swift添加页面+CoreDataProperties.swift
public var wrappedName: String { name?? "Unknown Title" } public var cardArray: [Card] { let set = toCard as? Set<Card>?? [] return set.sorted { $0.wrappedTitle < $1.wrappedTitle } }
Add in "Card+CoreDataProperties.swift"添加“Card+CoreDataProperties.swift”
public var wrappedTitle: String { title?? "Unknown Title" } public var wrappedPrice: Double { price }
Now you can use relationship's value like this!现在你可以像这样使用关系的值了!
ForEach(pages, id: \.self) { page in
if whichPage == page {
ForEach(page.cardArray, id: \.self) { card in
VStack {
CardView(price: card.wrappedPrice, title: card.wrappedTitle)
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.