SwiftUI Essentials
Last updated
Last updated
SwiftUI โฉ intro โฉ SwiftUI Essentials
SwiftUI Essentials (WWDC2024)
2:30 - Declarative views
Text("Whiskers")
Image(systemName: "cat.fill")
Button("Give Treat") {
// Give Whiskers a treat
}
2:43 - Declarative views: layout
HStack {
Label("Whiskers", systemImage: "cat.fill")
Spacer()
Text("Tightrope walking")
}
2:56 - Declarative views: list
struct ContentView: View {
@State private var pets = Pet.samplePets
var body: some View {
List(pets) { pet in
HStack {
Label("Whiskers", systemImage: "cat.fill")
Spacer()
Text("Tightrope walking")
}
}
}
}
struct Pet: Identifiable {
enum Kind {
case cat
case dog
case fish
case bird
case lizard
case turtle
case rabbit
case bug
var systemImage: String {
switch self {
case .cat: return "cat.fill"
case .dog: return "dog.fill"
case .fish: return "fish.fill"
case .bird: return "bird.fill"
case .lizard: return "lizard.fill"
case .turtle: return "tortoise.fill"
case .rabbit: return "rabbit.fill"
case .bug: return "ant.fill"
}
}
}
let id = UUID()
var name: String
var kind: Kind
var trick: String
init(_ name: String, kind: Kind, trick: String) {
self.name = name
self.kind = kind
self.trick = trick
}
static let samplePets = [
Pet("Whiskers", kind: .cat, trick: "Tightrope walking"),
Pet("Roofus", kind: .dog, trick: "Home runs"),
Pet("Bubbles", kind: .fish, trick: "100m freestyle"),
Pet("Mango", kind: .bird, trick: "Basketball dunk"),
Pet("Ziggy", kind: .lizard, trick: "Parkour"),
Pet("Sheldon", kind: .turtle, trick: "Kickflip"),
Pet("Chirpy", kind: .bug, trick: "Canon in D")
]
}
3:07 - Declarative views: list
struct ContentView: View {
@State private var pets = Pet.samplePets
var body: some View {
List(pets) { pet in
HStack {
Label(pet.name, systemImage: pet.kind.systemImage)
Spacer()
Text(pet.trick)
}
}
}
}
struct Pet: Identifiable {
enum Kind {
case cat
case dog
case fish
case bird
case lizard
case turtle
case rabbit
case bug
var systemImage: String {
switch self {
case .cat: return "cat.fill"
case .dog: return "dog.fill"
case .fish: return "fish.fill"
case .bird: return "bird.fill"
case .lizard: return "lizard.fill"
case .turtle: return "tortoise.fill"
case .rabbit: return "rabbit.fill"
case .bug: return "ant.fill"
}
}
}
let id = UUID()
var name: String
var kind: Kind
var trick: String
init(_ name: String, kind: Kind, trick: String) {
self.name = name
self.kind = kind
self.trick = trick
}
static let samplePets = [
Pet("Whiskers", kind: .cat, trick: "Tightrope walking"),
Pet("Roofus", kind: .dog, trick: "Home runs"),
Pet("Bubbles", kind: .fish, trick: "100m freestyle"),
Pet("Mango", kind: .bird, trick: "Basketball dunk"),
Pet("Ziggy", kind: .lizard, trick: "Parkour"),
Pet("Sheldon", kind: .turtle, trick: "Kickflip"),
Pet("Chirpy", kind: .bug, trick: "Canon in D")
]
}
4:24 - Declarative and imperative programming
struct ContentView: View {
@State private var pets = Pet.samplePets
var body: some View {
Button("Add Pet") {
pets.append(Pet("Toby", kind: .dog, trick: "WWDC Presenter"))
}
List(pets) { pet in
HStack {
Label(pet.name, systemImage: pet.kind.systemImage)
Spacer()
Text(pet.trick)
}
}
}
}
struct Pet: Identifiable {
enum Kind {
case cat
case dog
case fish
case bird
case lizard
case turtle
case rabbit
case bug
var systemImage: String {
switch self {
case .cat: return "cat.fill"
case .dog: return "dog.fill"
case .fish: return "fish.fill"
case .bird: return "bird.fill"
case .lizard: return "lizard.fill"
case .turtle: return "tortoise.fill"
case .rabbit: return "rabbit.fill"
case .bug: return "ant.fill"
}
}
}
let id = UUID()
var name: String
var kind: Kind
var trick: String
init(_ name: String, kind: Kind, trick: String) {
self.name = name
self.kind = kind
self.trick = trick
}
static let samplePets = [
Pet("Whiskers", kind: .cat, trick: "Tightrope walking"),
Pet("Roofus", kind: .dog, trick: "Home runs"),
Pet("Bubbles", kind: .fish, trick: "100m freestyle"),
Pet("Mango", kind: .bird, trick: "Basketball dunk"),
Pet("Ziggy", kind: .lizard, trick: "Parkour"),
Pet("Sheldon", kind: .turtle, trick: "Kickflip"),
Pet("Chirpy", kind: .bug, trick: "Canon in D")
]
}
5:33 - Layout container
HStack {
Label("Whiskers", systemImage: "cat.fill")
Spacer()
Text("Tightrope walking")
}