Swappen - @State und mutating

This commit is contained in:
Ingo Rohlf 2025-10-21 14:50:03 +02:00
parent ad7a4dc6c7
commit 077a0f183c
7 changed files with 140 additions and 3 deletions

View file

@ -9,9 +9,12 @@ struct TheSwiftWeek: App {
var body: some Scene { var body: some Scene {
WindowGroup { WindowGroup {
VStack{ VStack{
Text(.myVehicle)
TrainView(train: train)
TrainView(train: train2) Text(.myVehicle)
TrainView(train: train)
TrainView(train: train2)
Spacer()
} }
} }

31
ContentView.swift Normal file
View file

@ -0,0 +1,31 @@
import SwiftUI
enum TabLabel: String {
case bikes
case trains
case cars
var title: String { rawValue.capitalized }
}
struct ContentView: View {
var body: some View {
TabView {
Tab(TabLabel.bikes.title, systemImage: "bicycle") {
BikeView()
}
Tab(TabLabel.bikes.title, systemImage: "train.side.front.car") {
TrainView()
}
Tab("drücken", systemImage: "car") {
SwapperView()
}
}
}
}
#Preview {
ContentView()
}

View file

@ -1,6 +1,14 @@
{ {
"sourceLanguage" : "en", "sourceLanguage" : "en",
"strings" : { "strings" : {
"drücken" : {
"comment" : "A tab label for a tab that prints something.",
"isCommentAutoGenerated" : true
},
"Hello, World!" : {
"comment" : "A simple text view with padding applied to it.",
"isCommentAutoGenerated" : true
},
"My Vehicle" : { "My Vehicle" : {
"extractionState" : "manual", "extractionState" : "manual",
"localizations" : { "localizations" : {
@ -18,6 +26,22 @@
} }
} }
}, },
"Show Alert" : {
"comment" : "A button that triggers an alert when pressed.",
"isCommentAutoGenerated" : true
},
"Swapping %@ <-> %@" : {
"comment" : "A label displaying the current state of the swap operation, showing the values of the two variables before and after the swap.",
"isCommentAutoGenerated" : true,
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "new",
"value" : "Swapping %1$@ <-> %2$@"
}
}
}
},
"Train %@ has ^[%lld minute](inflect: true) delay." : { "Train %@ has ^[%lld minute](inflect: true) delay." : {
"comment" : "A localized string resource describing a train. The first argument is the name of the train. The second argument is the delay in minutes.", "comment" : "A localized string resource describing a train. The first argument is the name of the train. The second argument is the delay in minutes.",
"isCommentAutoGenerated" : true, "isCommentAutoGenerated" : true,
@ -48,6 +72,18 @@
} }
} }
} }
},
"Vorher %@ <-> %@" : {
"comment" : "Displays two text fields with the text \"Vorher\" and \"Nachher\" followed by a button that, when pressed, swaps the text in the two fields.",
"isCommentAutoGenerated" : true,
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "new",
"value" : "Vorher %1$@ <-> %2$@"
}
}
}
} }
}, },
"version" : "1.1" "version" : "1.1"

11
PaddingView.swift Normal file
View file

@ -0,0 +1,11 @@
import SwiftUI
struct PaddingView: View {
var body: some View {
Text("Hello, World!")
.padding()
}
}
#Preview {
PaddingView()
}

44
SwapperView.swift Normal file
View file

@ -0,0 +1,44 @@
import SwiftUI
struct SwapperView: View{
@State var sw = Swapper(a: "Apfel", b: "Banana")
@State var a = "Apfel"
@State var b = "Banane"
func tausche(){
let tmp = a
a = b
b = tmp
}
var body: some View {
VStack {
Text("Vorher \(a) <-> \(b)")
Button("Show Alert") {
print("Hello, World!")
tausche()
sw.tausche()
}
Text("Swapping \(sw.a) <-> \(sw.b)")
}
}
}
struct Swapper<T> {
var a: T
var b: T
mutating func tausche(){
let tmp = a
a = b
b = tmp
}
}
#Preview {
SwapperView()
}

View file

@ -11,6 +11,9 @@
0CC14A892E92EEA900271E8D /* Playground.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CC14A882E92EEA900271E8D /* Playground.swift */; }; 0CC14A892E92EEA900271E8D /* Playground.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CC14A882E92EEA900271E8D /* Playground.swift */; };
FBA00D6D2EA78411006F8B9A /* Localizable.xcstrings in Resources */ = {isa = PBXBuildFile; fileRef = FBA00D6C2EA78411006F8B9A /* Localizable.xcstrings */; }; FBA00D6D2EA78411006F8B9A /* Localizable.xcstrings in Resources */ = {isa = PBXBuildFile; fileRef = FBA00D6C2EA78411006F8B9A /* Localizable.xcstrings */; };
FBA00D6F2EA78853006F8B9A /* Color.swift in Sources */ = {isa = PBXBuildFile; fileRef = FBA00D6E2EA78850006F8B9A /* Color.swift */; }; FBA00D6F2EA78853006F8B9A /* Color.swift in Sources */ = {isa = PBXBuildFile; fileRef = FBA00D6E2EA78850006F8B9A /* Color.swift */; };
FBA00D712EA7A839006F8B9A /* PaddingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FBA00D702EA7A830006F8B9A /* PaddingView.swift */; };
FBA00D732EA7A96C006F8B9A /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FBA00D722EA7A96C006F8B9A /* ContentView.swift */; };
FBA00D752EA7ACE1006F8B9A /* SwapperView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FBA00D742EA7ACDD006F8B9A /* SwapperView.swift */; };
FBA6FA5E2EA63EA300C373EC /* Models.swift in Sources */ = {isa = PBXBuildFile; fileRef = FBA6FA5D2EA63EA300C373EC /* Models.swift */; }; FBA6FA5E2EA63EA300C373EC /* Models.swift in Sources */ = {isa = PBXBuildFile; fileRef = FBA6FA5D2EA63EA300C373EC /* Models.swift */; };
FBA6FA602EA66C2E00C373EC /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = FBA6FA5F2EA66C2E00C373EC /* Assets.xcassets */; }; FBA6FA602EA66C2E00C373EC /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = FBA6FA5F2EA66C2E00C373EC /* Assets.xcassets */; };
FBA6FA622EA76AAD00C373EC /* BikeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FBA6FA612EA76AAD00C373EC /* BikeView.swift */; }; FBA6FA622EA76AAD00C373EC /* BikeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FBA6FA612EA76AAD00C373EC /* BikeView.swift */; };
@ -24,6 +27,9 @@
0CC14A882E92EEA900271E8D /* Playground.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Playground.swift; sourceTree = "<group>"; }; 0CC14A882E92EEA900271E8D /* Playground.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Playground.swift; sourceTree = "<group>"; };
FBA00D6C2EA78411006F8B9A /* Localizable.xcstrings */ = {isa = PBXFileReference; lastKnownFileType = text.json.xcstrings; path = Localizable.xcstrings; sourceTree = "<group>"; }; FBA00D6C2EA78411006F8B9A /* Localizable.xcstrings */ = {isa = PBXFileReference; lastKnownFileType = text.json.xcstrings; path = Localizable.xcstrings; sourceTree = "<group>"; };
FBA00D6E2EA78850006F8B9A /* Color.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Color.swift; sourceTree = "<group>"; }; FBA00D6E2EA78850006F8B9A /* Color.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Color.swift; sourceTree = "<group>"; };
FBA00D702EA7A830006F8B9A /* PaddingView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaddingView.swift; sourceTree = "<group>"; };
FBA00D722EA7A96C006F8B9A /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
FBA00D742EA7ACDD006F8B9A /* SwapperView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwapperView.swift; sourceTree = "<group>"; };
FBA6FA5D2EA63EA300C373EC /* Models.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Models.swift; sourceTree = "<group>"; }; FBA6FA5D2EA63EA300C373EC /* Models.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Models.swift; sourceTree = "<group>"; };
FBA6FA5F2EA66C2E00C373EC /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; }; FBA6FA5F2EA66C2E00C373EC /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
FBA6FA612EA76AAD00C373EC /* BikeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BikeView.swift; sourceTree = "<group>"; }; FBA6FA612EA76AAD00C373EC /* BikeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BikeView.swift; sourceTree = "<group>"; };
@ -45,9 +51,12 @@
0CC14A6D2E92EC4700271E8D = { 0CC14A6D2E92EC4700271E8D = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
FBA00D742EA7ACDD006F8B9A /* SwapperView.swift */,
FBA00D702EA7A830006F8B9A /* PaddingView.swift */,
FBA00D6E2EA78850006F8B9A /* Color.swift */, FBA00D6E2EA78850006F8B9A /* Color.swift */,
FBA6FA632EA7715000C373EC /* Extentions.swift */, FBA6FA632EA7715000C373EC /* Extentions.swift */,
0CC14A842E92EC7A00271E8D /* App.swift */, 0CC14A842E92EC7A00271E8D /* App.swift */,
FBA00D722EA7A96C006F8B9A /* ContentView.swift */,
FBA6FA5F2EA66C2E00C373EC /* Assets.xcassets */, FBA6FA5F2EA66C2E00C373EC /* Assets.xcassets */,
0CC14A882E92EEA900271E8D /* Playground.swift */, 0CC14A882E92EEA900271E8D /* Playground.swift */,
FBA6FA612EA76AAD00C373EC /* BikeView.swift */, FBA6FA612EA76AAD00C373EC /* BikeView.swift */,
@ -143,10 +152,13 @@
files = ( files = (
FBA6FA5E2EA63EA300C373EC /* Models.swift in Sources */, FBA6FA5E2EA63EA300C373EC /* Models.swift in Sources */,
FBA6FA662EA7725A00C373EC /* TrainView.swift in Sources */, FBA6FA662EA7725A00C373EC /* TrainView.swift in Sources */,
FBA00D752EA7ACE1006F8B9A /* SwapperView.swift in Sources */,
0CC14A892E92EEA900271E8D /* Playground.swift in Sources */, 0CC14A892E92EEA900271E8D /* Playground.swift in Sources */,
FBA6FA642EA7715E00C373EC /* Extentions.swift in Sources */, FBA6FA642EA7715E00C373EC /* Extentions.swift in Sources */,
0CC14A872E92EC7B00271E8D /* App.swift in Sources */, 0CC14A872E92EC7B00271E8D /* App.swift in Sources */,
FBA00D732EA7A96C006F8B9A /* ContentView.swift in Sources */,
FBA00D6F2EA78853006F8B9A /* Color.swift in Sources */, FBA00D6F2EA78853006F8B9A /* Color.swift in Sources */,
FBA00D712EA7A839006F8B9A /* PaddingView.swift in Sources */,
FBA6FA622EA76AAD00C373EC /* BikeView.swift in Sources */, FBA6FA622EA76AAD00C373EC /* BikeView.swift in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;