User Tools

Site Tools


swift

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
swift [2023/11/19 22:13]
admin
swift [2023/11/19 22:16] (current)
admin
Line 77: Line 77:
 [[https://​developer.apple.com/​library/​archive/​documentation/​General/​Conceptual/​iCloudDesignGuide/​Chapters/​DesigningForKey-ValueDataIniCloud.html#//​apple_ref/​doc/​uid/​TP40012094-CH7|Apple:​ Designing for Key-Value Data in iCloud]] [[https://​developer.apple.com/​library/​archive/​documentation/​General/​Conceptual/​iCloudDesignGuide/​Chapters/​DesigningForKey-ValueDataIniCloud.html#//​apple_ref/​doc/​uid/​TP40012094-CH7|Apple:​ Designing for Key-Value Data in iCloud]]
  
-### Relationship ​Between Entities ​in CoreData+### Relationship: Connect entities ​in CoreData
 ref: https://​www.hackingwithswift.com/​books/​ios-swiftui/​one-to-many-relationships-with-core-data-swiftui-and-fetchrequest ref: https://​www.hackingwithswift.com/​books/​ios-swiftui/​one-to-many-relationships-with-core-data-swiftui-and-fetchrequest
-Step by Step: 
  
 1. Create Entities in CoreData 1. Create Entities in CoreData
Line 87: Line 86:
 5. In `...CoreDataProperties` file, you should be able to see code like below for adding transactions to this certain piggy object. 5. In `...CoreDataProperties` file, you should be able to see code like below for adding transactions to this certain piggy object.
  
-```swift+```
 extension Piggy { extension Piggy {
  
Line 121: Line 120:
 2. In  ` ...CoreDataProperties` file, `transactions` is an `NSSet`, perform the following steps to convert it from an `NSSet` to a `Set<​PurchasedItem>​` – a Swift-native type where the types of its contents is specificy to `PurchasedItem` type. Convert that `Set<​PurchasedItem>​` into an array, so that `ForEach` can read individual values from there. Sort that array, so the transactions come in a sensible order. 2. In  ` ...CoreDataProperties` file, `transactions` is an `NSSet`, perform the following steps to convert it from an `NSSet` to a `Set<​PurchasedItem>​` – a Swift-native type where the types of its contents is specificy to `PurchasedItem` type. Convert that `Set<​PurchasedItem>​` into an array, so that `ForEach` can read individual values from there. Sort that array, so the transactions come in a sensible order.
  
-```swift+```
    ​public var transactionArray:​ [PurchasedItem] {    ​public var transactionArray:​ [PurchasedItem] {
        let set = transactions as? Set<​PurchasedItem>​ ?? []        let set = transactions as? Set<​PurchasedItem>​ ?? []
Line 133: Line 132:
 1. Use them in your View: To read purchased items connected to a certain piggy in a View. Use the following code: 1. Use them in your View: To read purchased items connected to a certain piggy in a View. Use the following code:
  
-```swift+```
    ​struct HistoryView:​ View {    ​struct HistoryView:​ View {
        ​@Environment(\.managedObjectContext) private var viewContext        ​@Environment(\.managedObjectContext) private var viewContext
Line 165: Line 164:
 Or use the following code. However with this method the HistoryView won't update if you add new purchasedItem (transaction) because it is only monitoring the object `piggy`. To trigger view update when purchasedItem is added, you need to use the method above to fetch `PurchasedItem` directly. Or use the following code. However with this method the HistoryView won't update if you add new purchasedItem (transaction) because it is only monitoring the object `piggy`. To trigger view update when purchasedItem is added, you need to use the method above to fetch `PurchasedItem` directly.
  
-```swift+```
 struct HistoryView:​ View { struct HistoryView:​ View {
    ​@Environment(\.managedObjectContext) private var viewContext    ​@Environment(\.managedObjectContext) private var viewContext
swift.1700403186.txt.gz · Last modified: 2023/11/19 22:13 by admin