swift
86 lines · 1 tab
Sofia Martinez
Jan 2026
1 tab
import SwiftUI
// MARK: - State and Binding
struct CounterView: View {
@State private var count = 0
var body: some View {
VStack(spacing: 20) {
Text("Count: \(count)")
.font(.largeTitle)
CounterControls(count: $count)
}
}
}
struct CounterControls: View {
@Binding var count: Int
var body: some View {
HStack(spacing: 20) {
Button("-") { count -= 1 }
Button("+") { count += 1 }
}
.buttonStyle(.bordered)
}
}
// MARK: - ObservableObject and StateObject
class UserSettings: ObservableObject {
@Published var username = ""
@Published var isDarkMode = false
@Published var notificationsEnabled = true
static let shared = UserSettings()
}
struct SettingsView: View {
@StateObject private var settings = UserSettings()
var body: some View {
Form {
TextField("Username", text: $settings.username)
Toggle("Dark Mode", isOn: $settings.isDarkMode)
Toggle("Notifications", isOn: $settings.notificationsEnabled)
}
.environmentObject(settings)
}
}
// MARK: - Environment and EnvironmentObject
struct ProfileView: View {
@EnvironmentObject var settings: UserSettings
@Environment(\.colorScheme) var colorScheme
@Environment(\.dismiss) var dismiss
var body: some View {
VStack {
Text("Hello, \(settings.username)")
Text("Current theme: \(colorScheme == .dark ? "Dark" : "Light")")
Button("Close") {
dismiss()
}
}
}
}
// MARK: - AppStorage
struct PreferencesView: View {
@AppStorage("fontSize") private var fontSize = 16.0
@AppStorage("showImages") private var showImages = true
@AppStorage("lastLoginDate") private var lastLoginDate = Date()
var body: some View {
Form {
Slider(value: $fontSize, in: 12...24) {
Text("Font Size: \(Int(fontSize))")
}
Toggle("Show Images", isOn: $showImages)
Text("Last login: \(lastLoginDate.formatted())")
}
}
}
1 file · swift
Explain with highlit
SwiftUI property wrappers manage state and data flow declaratively. @State creates mutable state owned by a view for simple values. @Binding creates two-way connections to pass state down the hierarchy without ownership. @ObservedObject watches external ObservableObject instances for changes. @StateObject creates and owns ObservableObject instances with proper lifecycle. @EnvironmentObject provides dependency injection across the view tree. @Environment accesses system values like color scheme or size category. @AppStorage persists simple values to UserDefaults. @Published marks properties that trigger view updates. Understanding which wrapper to use prevents memory leaks and ensures correct data flow.
Related snips
swift
import Combine
import Foundation
class SearchViewModel: ObservableObject {
@Published var searchQuery = ""
@Published var results: [SearchResult] = []
Combine operators for data transformation
swift
combine
reactive
by Sofia Martinez
1 tab
swift
import SwiftUI
struct ContentView: View {
@State private var username = ""
@State private var isLoggedIn = false
@StateObject private var viewModel = LoginViewModel()
SwiftUI declarative UI with state management
swift
swiftui
ios
by Sofia Martinez
2 tabs
swift
import SwiftUI
struct CardModifier: ViewModifier {
var backgroundColor: Color = .white
var cornerRadius: CGFloat = 12
var shadowRadius: CGFloat = 5
Custom SwiftUI view modifiers for reusability
swift
swiftui
ios
by Sofia Martinez
2 tabs
swift
import Foundation
import UIKit
class ImageProcessor {
// Background processing with main thread updates
func processImage(_ image: UIImage, completion: @escaping (UIImage?) -> Void) {
Grand Central Dispatch for concurrency
swift
gcd
concurrency
by Sofia Martinez
1 tab
swift
import SwiftUI
enum Route: Hashable {
case postDetail(id: Int)
case userProfile(id: Int)
case settings
Navigation patterns with NavigationStack
swift
swiftui
navigation
by Sofia Martinez
2 tabs
swift
import UIKit
import WebKit
class WebViewController: UIViewController {
private var webView: WKWebView!
private var progressView: UIProgressView!
WKWebView for web content display
swift
wkwebview
webkit
by Sofia Martinez
1 tab
Share this code
Here's the card — post it anywhere.