User Tools

Site Tools


swift

**This is an old revision of the document!**

Swift

Tutorial for beginners: https://developer.apple.com/tutorials/app-dev-training

Important concept: pass object between views.

struct ScrumsView_Previews: PreviewProvider is just for showing an mock up preview when you coding. It is not a part of the real app.

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(_:) means …

Property wrappers is those code started with @ than you add before declearing an variable or object. For example @EnvironmentObject var time: Float.

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. Use continuations to bridge between asynchronous functions and existing callback-based functions.

Class

@Published wrapper

In this code, Why there needs to be a @Published wrapper?

class MyStore: ObservableObject {
    @Published var selection:Int?

    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: View {
    @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
           Text("Current ID: \(currentID)")
        }
    }
}

class MyStore: ObservableObject {
    @Published var selection:Int?

    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, but the value is shared across different devices for the same app, that ties to an Apple ID account.

ref: Apple: iCloud Design Guide Apple: Designing for Key-Value Data in iCloud

Icons

How to import and use custom icons:

  1. Import the svg file into Assets in Xcode
  2. Use it in the code, for example: barItem: UITabBarItem(title: "Piggy", image: UIImage(named: "piggy.saving"))

Quick Reference

swift.1698237935.txt.gz · Last modified: 2023/10/25 20:45 by admin