swift
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| swift [2023/10/25 20:45] โ admin | swift [2023/11/19 22:16] (current) โ admin | ||
|---|---|---|---|
| Line 76: | Line 76: | ||
| [[https:// | [[https:// | ||
| [[https:// | [[https:// | ||
| + | |||
| + | ### Relationship: | ||
| + | ref: https:// | ||
| + | |||
| + | 1. Create Entities in CoreData | ||
| + | 2. Select the relationship created, in the right tool panel, change `Type` to `To Many` | ||
| + | 3. Select the entity, in the right tool panel, change `Codegen` to `Manual/ | ||
| + | 4. Use `Editor> | ||
| + | 5. In `...CoreDataProperties` file, you should be able to see code like below for adding transactions to this certain piggy object. | ||
| + | |||
| + | ``` | ||
| + | extension Piggy { | ||
| + | |||
| + | | ||
| + | | ||
| + | } | ||
| + | |||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | } | ||
| + | extension Piggy { | ||
| + | @objc(addTransactionsObject: | ||
| + | @NSManaged public func addToTransactions(_ value: PurchasedItem) | ||
| + | |||
| + | @objc(removeTransactionsObject: | ||
| + | @NSManaged public func removeFromTransactions(_ value: PurchasedItem) | ||
| + | |||
| + | @objc(addTransactions: | ||
| + | @NSManaged public func addToTransactions(_ values: NSSet) | ||
| + | |||
| + | @objc(removeTransactions: | ||
| + | @NSManaged public func removeFromTransactions(_ values: NSSet) | ||
| + | } | ||
| + | ``` | ||
| + | |||
| + | 1. Edit ` ...CoreDataProperties` file, add code like `public var wrappedCapacity: | ||
| + | 2. In ` ...CoreDataProperties` file, `transactions` is an `NSSet`, perform the following steps to convert it from an `NSSet` to a `Set< | ||
| + | |||
| + | ``` | ||
| + | | ||
| + | let set = transactions as? Set< | ||
| + | |||
| + | | ||
| + | | ||
| + | } | ||
| + | } | ||
| + | ``` | ||
| + | |||
| + | 1. Use them in your View: To read purchased items connected to a certain piggy in a View. Use the following code: | ||
| + | |||
| + | ``` | ||
| + | | ||
| + | | ||
| + | | ||
| + | let piggy: Piggy | ||
| + | |||
| + | | ||
| + | |||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | ) | ||
| + | | ||
| + | } | ||
| + | |||
| + | var body: some View { | ||
| + | | ||
| + | if (items.count != 0) { | ||
| + | List { | ||
| + | | ||
| + | // view related code | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | ``` | ||
| + | |||
| + | 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. | ||
| + | |||
| + | ``` | ||
| + | struct HistoryView: | ||
| + | | ||
| + | | ||
| + | let piggy: Piggy | ||
| + | |||
| + | var body: some View { | ||
| + | | ||
| + | if (piggy.transactionArrary.count != 0) { | ||
| + | List { | ||
| + | | ||
| + | // view related code | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | ``` | ||
| + | |||
| + | 1. To write new purchased item and connect it to piggy. | ||
| + | |||
| + | ```swiftUI | ||
| + | let newPurchase = PurchasedItem(context: | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | try? viewContext.save() | ||
| + | ``` | ||
| + | |||
| + | |||
| ## Icons | ## Icons | ||
swift.1698237935.txt.gz ยท Last modified: (external edit)
