简体   繁体   English

如何使用 SwiftUI 在 CoreData 中的 ForEach 中使用关系?

[英]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:编辑:

  1. Goto.xcdatamodeld file Goto.xcdatamodeld 文件

  2. Click Entity -> Class -> Codezen to Manual/None单击实体 -> Class -> Codezen 到手动/无

  3. Editor -> Create NSManagedObject Subclass编辑器 -> 创建 NSManagedObject 子类

  4. 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 } }
  5. Add in "Card+CoreDataProperties.swift"添加“Card+CoreDataProperties.swift”

     public var wrappedTitle: String { title?? "Unknown Title" } public var wrappedPrice: Double { price }
  6. 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.

相关问题 如何在Swift 2中将coreData与一对多关系使用? - How can I use a one-to-many relationship with coreData with Swift 2? 无法使用 SwiftUI 中的 ForEach 和 CoreData 将数据正确传递给模态演示 - Can’t pass data correctly to modal presentation using ForEach and CoreData in SwiftUI 如何使用一对多关系将对象与CoreData中的另一个对象连接? - How can I attach an object with another object in CoreData using a one to many relationship? 如何使用 NSPredicate 在 Swift 5 中使用 CoreData 搜索关系属性的 ObjectID? - How to use NSPredicate to search for ObjectID of a relationship attribute using CoreData in Swift 5? 如何使用sqlitebrowser工具查看CoreData对多关系? - How can I view a CoreData to-many relationship with the sqlitebrowser tool? 如何在 SwiftUI 中使用 Foreach 视图? - How to use Foreach on view in SwiftUI? 解决与 SwiftUI ForEach 一起使用的可选一对一关系? - Resolving optional one-to-one relationship for use with SwiftUI ForEach? CoreData • 如何使用Swift 5 修改CoreData 中的多对多关系? - CoreData • How to modify a many-to-many-Relationship in CoreData using Swift 5? 为什么我不能在NSManagedObject子类的计算属性中使用关系? (CoreData,快速) - Why can't I use a relationship in a NSManagedObject subclass computed property? (CoreData, swift) 如何在SwiftUI中使用UIViewController和UIView? - How can I use UIViewController and UIView in SwiftUI?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM