swift
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| swift [2022/08/07 23:37] – admin | swift [2023/11/19 22:16] (current) – admin | ||
|---|---|---|---|
| Line 3: | Line 3: | ||
| Tutorial for beginners: https:// | Tutorial for beginners: https:// | ||
| - | | + | Important concept: pass object between views. |
| - | * `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. | + | `struct ScrumsView_Previews: |
| - | * The annotation `environmentObject(_: | + | |
| - | * Property wrappers is those code started with `@` than you add before declearing an variable or object. For example @EnvironmentObject var time: Float | + | 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. |
| + | |||
| + | |||
| + | The annotation `environmentObject(_: | ||
| + | |||
| + | Property wrappers is those code started with `@` than you add before declearing an variable or object. For example | ||
| + | |||
| + | Scenes are like windows. active — A scene is in the foreground, and the user can interact with it. | ||
| + | inactive — The scene is visible, but the system disabled interaction with the scene. For example, in multitasking mode, you can see your app’s panel alongside others, but it isn’t the active panel. | ||
| + | background — The app is running, but the scene isn’t visible in the UI. A scene enters this phase prior to app termination. A common use is to trigger an action that saves app data when the scene phase becomes inactive. | ||
| + | |||
| + | Task modifier for async loading contents in the background. [ref](https:// | ||
| + | |||
| + | ## Class | ||
| + | |||
| + | ### @Published wrapper | ||
| + | In this code, Why there needs to be a @Published wrapper? | ||
| + | |||
| + | ``` | ||
| + | class MyStore: ObservableObject { | ||
| + | @Published var selection: | ||
| + | |||
| + | func sendID(_ id: Int) { | ||
| + | self.selection = id | ||
| + | } | ||
| + | } | ||
| + | ``` | ||
| + | |||
| + | The @Published wrapper is used in SwiftUI to automatically notify any views that are observing the property when its value changes. | ||
| + | |||
| + | In the given code, the selection property is marked as @Published, indicating that it is an observable property. When the sendID(_:) function is called and the value of selection is updated with a new ID, the @Published wrapper will automatically send notifications to any views that are observing this property. These views will then be updated accordingly to reflect the new value of selection. | ||
| + | |||
| + | By using the @Published wrapper, you eliminate the need to manually send notifications and manage observers, making it easier to update and synchronize data between different parts of your SwiftUI app. | ||
| + | |||
| + | |||
| + | ``` | ||
| + | struct ClosureDemo: | ||
| + | @StateObject var store = MyStore() | ||
| + | var body: some View { | ||
| + | if let currentID = store.selection { // currentID won't be updated if there is no @Publish wrapper in MyStore class | ||
| + | | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | class MyStore: ObservableObject { | ||
| + | @Published var selection: | ||
| + | |||
| + | func sendID(_ id: Int) { | ||
| + | self.selection = id | ||
| + | } | ||
| + | } | ||
| + | ``` | ||
| + | |||
| + | ## Data Storage | ||
| + | |||
| + | ### UserDefaults | ||
| + | is used to store small user preferences and application settings. It has been available as part of the Foundation framework in iOS and macOS long before SwiftUI was introduced. | ||
| + | |||
| + | ### AppStorage | ||
| + | is just a wrapper of UserDefaults in Swift UI. | ||
| + | |||
| + | ### iCloud Key-Value store (NSUbiquitousKeyValueStore) | ||
| + | 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: | ||
| + | |||
| + | |||
| + | |||
| + | ## Quick Reference | ||
| + | {{:: | ||
| + | |||
| + | {{: | ||
swift.1659886641.txt.gz · Last modified: (external edit)
