Skip to content

Instantly share code, notes, and snippets.

@foxicode
foxicode / ContentView.swift
Created August 24, 2023 20:51
ContentView from SimpleToDo App
//
// ContentView.swift
// SimpleToDo
//
// Copyright © 2022 Paul Hudson.
// Licensed under MIT license.
//
// https://github.com/twostraws/simple-swiftui
// See LICENSE for license information
//
@foxicode
foxicode / SimpleToDoApp.swift
Last active August 24, 2023 20:52
SimpleToDoApp main app file
//
// SimpleToDoApp.swift
// SimpleToDo
//
// Copyright © 2022 Paul Hudson.
// Licensed under MIT license.
//
// https://github.com/twostraws/simple-swiftui
// See LICENSE for license information
//
@foxicode
foxicode / Existentials.swift
Created August 21, 2023 13:22
Existentials in Swift
protocol Shape {
associatedtype NumericType
func area() -> NumericType
}
struct Rectangle: Shape {
typealias NumericType = Double
var width: Double
@foxicode
foxicode / NavigationStackExample.swift
Created August 21, 2023 01:11
NavigationStack SwiftUI example
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationStack {
List {
NavigationLink("Success", value: Target.successScreen)
NavigationLink("Error", value: Target.errorScreen)
}
.navigationDestination(for: Target.self) { target in
@foxicode
foxicode / NavigationWithDelay.swift
Created August 21, 2023 01:02
Navigation with data loading
import SwiftUI
struct ContentView: View {
@State var loadedContent: String?
var isActive: Binding<Bool> {
Binding(
get: { loadedContent != nil },
set: { _ in loadedContent = nil }
)
@foxicode
foxicode / Navigation.swift
Last active August 21, 2023 00:32
Navigation in SwiftUI
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
VStack {
Text("Original content view")
NavigationLink {
InnerContentView()
} label: {
@foxicode
foxicode / App.swift
Created August 18, 2023 23:07
Using AppDelegate in SwiftUI
import SwiftUI
@main
struct Swift2TestApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
}
@foxicode
foxicode / App.swift
Created August 18, 2023 23:02
App lifecycle handling in SwiftUI
import SwiftUI
@main
struct Swift2TestApp: App {
@Environment(\.scenePhase) private var scenePhase
var body: some Scene {
WindowGroup {
ContentView()
}.onChange(of: scenePhase, perform: { scenePhase in
@foxicode
foxicode / App.swift
Created August 18, 2023 22:38
SwiftUI 2 App example
import SwiftUI
@main
struct Swift2TestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
@foxicode
foxicode / SceneDelegate.swift
Created August 18, 2023 22:11
SceneDelegate example
import UIKit
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
guard let _ = (scene as? UIWindowScene) else { return }