swift
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| swift [2023/10/22 11:34] โ admin | swift [2023/11/19 22:16] (current) โ admin | ||
|---|---|---|---|
| Line 6: | Line 6: | ||
| `struct ScrumsView_Previews: | `struct ScrumsView_Previews: | ||
| - | |||
| - | {{: | ||
| Add `@State` at the beginning to declear a single source of truth at the very most parent view. Use `$[variable name]` to pass it to child view. In child view initialize with `@Binding var [variable name]: [variable type]` to use that data. | Add `@State` at the beginning to declear a single source of truth at the very most parent view. Use `$[variable name]` to pass it to child view. In child view initialize with `@Binding var [variable name]: [variable type]` to use that data. | ||
| Line 73: | Line 71: | ||
| ### iCloud Key-Value store (NSUbiquitousKeyValueStore) | ### iCloud Key-Value store (NSUbiquitousKeyValueStore) | ||
| You can think of it like UserDefaults, | You can think of it like UserDefaults, | ||
| + | |||
| + | |||
| + | ref: | ||
| + | [[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 | ||
| + | |||
| + | How to import and use custom icons: | ||
| + | |||
| + | 1. Download icons svg that is made in proper format: https:// | ||
| + | 2. Import the svg file into Assets in Xcode | ||
| + | 3. Use it in the code, for example: `barItem: UITabBarItem(title: | ||
| + | |||
swift.1697945673.txt.gz ยท Last modified: (external edit)
