import SwiftUI
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
struct ContentView: View {
var body: some View {
Text("Hello, World!")
}
} struct CounterView: View {
@State private var count = 0
var body: some View {
VStack {
Text("Count: \(count)")
Button("Increment") {
count += 1
}
}
}
} // Text
Text("Hello")
.font(.title)
.fontWeight(.bold)
.foregroundColor(.blue)
.lineLimit(2)
// Image
Image(systemName: "star.fill")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 100, height: 100)
Image("photo")
.clipShape(Circle())
.overlay(Circle().stroke(.white, lineWidth: 2))
.shadow(radius: 5) // Button
Button("Tap me") {
print("Tapped")
}
.buttonStyle(.borderedProminent)
Button(action: doSomething) {
Label("Add", systemImage: "plus")
}
// TextField
@State private var text = ""
TextField("Placeholder", text: $text)
.textFieldStyle(.roundedBorder)
// SecureField
SecureField("Password", text: $password)
// Toggle
@State private var isOn = false
Toggle("Enable", isOn: $isOn) // Vertical stack
VStack(alignment: .leading, spacing: 10) {
Text("First")
Text("Second")
Text("Third")
}
// Horizontal stack
HStack(spacing: 20) {
Image(systemName: "star")
Text("Rating")
Spacer()
Text("5.0")
}
// Depth stack (overlay)
ZStack {
Color.blue
Text("On top")
} // List
List {
ForEach(items) { item in
Text(item.name)
}
.onDelete(perform: deleteItems)
}
.listStyle(.insetGrouped)
// LazyVGrid
let columns = [
GridItem(.flexible()),
GridItem(.flexible())
]
LazyVGrid(columns: columns, spacing: 20) {
ForEach(items) { item in
ItemView(item: item)
}
}
// ScrollView
ScrollView {
VStack {
ForEach(0..<100) { i in
Text("Row \(i)")
}
}
} Text("Hello")
.padding()
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(10)
.shadow(radius: 5)
.frame(width: 200, height: 100)
.opacity(0.8)
.rotationEffect(.degrees(45))
.scaleEffect(1.5) Text("Hello")
.foregroundColor(isActive ? .blue : .gray)
.fontWeight(isImportant ? .bold : .regular)
// Custom modifier
struct CardModifier: ViewModifier {
func body(content: Content) -> some View {
content
.padding()
.background(Color.white)
.cornerRadius(10)
.shadow(radius: 5)
}
}
extension View {
func cardStyle() -> some View {
modifier(CardModifier())
}
}
// Usage
Text("Card").cardStyle() // @State - local state
@State private var count = 0
// @Binding - two-way binding from parent
struct ChildView: View {
@Binding var value: Int
}
// @StateObject - create observable object
@StateObject private var viewModel = MyViewModel()
// @ObservedObject - observe from parent
struct ChildView: View {
@ObservedObject var viewModel: MyViewModel
}
// @EnvironmentObject - shared state
@EnvironmentObject var settings: Settings class UserViewModel: ObservableObject {
@Published var name = ""
@Published var email = ""
@Published var isLoading = false
func fetchUser() async {
isLoading = true
// Fetch data...
isLoading = false
}
}
struct ProfileView: View {
@StateObject private var viewModel = UserViewModel()
var body: some View {
VStack {
if viewModel.isLoading {
ProgressView()
} else {
Text(viewModel.name)
}
}
.task {
await viewModel.fetchUser()
}
}
} struct ContentView: View {
@State private var data: [Item] = []
var body: some View {
List(data) { item in
Text(item.name)
}
.task {
// Runs when view appears
data = await fetchData()
}
.refreshable {
// Pull to refresh
data = await fetchData()
}
}
func fetchData() async -> [Item] {
let url = URL(string: "https://api.example.com/items")!
let (data, _) = try! await URLSession.shared.data(from: url)
return try! JSONDecoder().decode([Item].self, from: data)
}
}