diff --git a/App.swift b/App.swift index 19b512a..01c4978 100644 --- a/App.swift +++ b/App.swift @@ -11,7 +11,7 @@ struct TheSwiftWeek: App { VStack{ - CountView() + ContentView() } } diff --git a/ContentView.swift b/ContentView.swift index d361aa2..55c25a1 100644 --- a/ContentView.swift +++ b/ContentView.swift @@ -17,9 +17,9 @@ enum TabLabel: String { } } } -func tab(_ contentType: C.Type) +func tab(_ contentType: C.Type ) -> Tab { - Tab(C.title, systemImage: C.image) { + Tab(C.title, systemImage: C.image ) { C() } } @@ -27,24 +27,28 @@ func tab(_ contentType: C.Type) struct ContentView: View { + @AppStorage("tabSelection") var tabSelection = TabLabel.bikes + + var body: some View { - TabView { - Tab(TabLabel.bikes.title, systemImage: TabLabel.bikes.image) { - BikeView() - } - Tab(TabLabel.bikes.title, systemImage: TabLabel.trains.image) { - TrainView() - } - Tab("drücken", systemImage: TabLabel.cars.image) { - SwapperView() - } + //TabView(selection: $tabSelection) { + TabView() { +// Tab(TabLabel.bikes.title, systemImage: TabLabel.bikes.image) { +// BikeView() +// } +// Tab(TabLabel.bikes.title, systemImage: TabLabel.trains.image) { +// TrainView() +// } +// Tab("drücken", systemImage: TabLabel.cars.image) { +// SwapperView() +// } tab(ListView.self) tab(CountView.self) tab(BikeView.self) tab(TrainView.self) tab(SwapperView.self) tab(PaddingView.self) - } + }.tabViewStyle(.sidebarAdaptable) } } diff --git a/CountView.swift b/CountView.swift index e504f50..6c0e21c 100644 --- a/CountView.swift +++ b/CountView.swift @@ -1,34 +1,34 @@ -// -// CountView.swift -// TheSwiftWeek -// -// Created by Ingo Rohlf on 21.10.25. -// - import SwiftUI struct CountView: TabContent { static var title = "Count" static var image = "42.circle" - - @State var text = "" - + + @State var text = "🐼" + + @FocusState var isFocused: Bool // 1 + var body: some View { TextField("Enter Emoji", text: $text) .padding() - + .focused($isFocused) // 2 + .onAppear { + isFocused = true // 3 + } Text(""" - Zeichenzahl: \(text.count) - Unicode: \(text.unicodeScalars.count) - UTF-8 Bytes: \(text.lengthOfBytes(using: .utf8)) - """) - + count: \(text.count) + unicode: \(text.unicodeScalars.count) + length: \(text.lengthOfBytes(using: .utf8)) + """) + .background(.yellow) + .multilineTextAlignment(.center) + .bold() + .fontDesign(.rounded) + .font(.largeTitle) Spacer() } } - - #Preview { CountView() } diff --git a/ListView.swift b/ListView.swift index 5f427ca..6fd3b84 100644 --- a/ListView.swift +++ b/ListView.swift @@ -23,14 +23,22 @@ struct ListView: TabContent{ } List(bikes, id: \.name){ bike in HStack { + Color(bike.color) + //Circle().fill(style: bike.color) + + Text(String(bike.name)) + Spacer() } } List(bikes, id: \.name){ Text(String($0.price)) - - + } + + // Weil Vehicle jetzt Identifiable implementiert, geht es ohne id: \.name + List(bikes){ + Text(String($0.name)) } } diff --git a/Localizable.xcstrings b/Localizable.xcstrings index 2da0d9e..eed91a9 100644 --- a/Localizable.xcstrings +++ b/Localizable.xcstrings @@ -1,28 +1,32 @@ { "sourceLanguage" : "en", "strings" : { - " Zeichenzahl: %lld\n Unicode: %lld\n UTF-8 Bytes: %lld" : { - "comment" : "A block of text showing the number of characters, Unicode scalars, and UTF-8 bytes in the text entered by the user.", + "%lld" : { + "comment" : "A text element displaying a number. The content of the text element is the integer passed to this view's initializer.", + "isCommentAutoGenerated" : true + }, + "Buzz" : { + "comment" : "A word displayed in a list item.", + "isCommentAutoGenerated" : true + }, + "count: %lld\nunicode: %lld\nlength: %lld" : { + "comment" : "Displays information about the number of characters, Unicode scalars, and bytes in a given string.", "isCommentAutoGenerated" : true, "localizations" : { "en" : { "stringUnit" : { "state" : "new", - "value" : " Zeichenzahl: %1$lld\n Unicode: %2$lld\n UTF-8 Bytes: %3$lld" + "value" : "count: %1$lld\nunicode: %2$lld\nlength: %3$lld" } } } }, - "drücken" : { - "comment" : "A tab label for a tab that prints something.", - "isCommentAutoGenerated" : true - }, "Enter Emoji" : { "comment" : "A label for an text field where the user can enter an emoji.", "isCommentAutoGenerated" : true }, - "Hello, World!" : { - "comment" : "A simple text view with padding applied to it.", + "Fizz" : { + "comment" : "A word that is displayed when the number is a multiple of 3.", "isCommentAutoGenerated" : true }, "My Vehicle" : { diff --git a/Models.swift b/Models.swift index 24c5a49..c4019d7 100644 --- a/Models.swift +++ b/Models.swift @@ -2,18 +2,27 @@ import Foundation import SwiftUI -protocol Vehicle { +protocol Vehicle: Identifiable { var name: String { get } var color: Color { get set } } - -protocol TabContent: View { - init() - static var title: String { get } - static var image: String { get } +extension Vehicle { + var id: UUID {UUID()} } -struct Train: Vehicle, CustomStringConvertible , CustomLocalizedStringResourceConvertible { +//protocol TabContent: View { +protocol TabContent: View { + init() + static var title: String { get } + static var image: String { get } + +} +extension TabContent { + var id: UUID {UUID()} +} + + +struct Train: @MainActor Vehicle, CustomStringConvertible , CustomLocalizedStringResourceConvertible { var name: String var color: Color var delay: Int @@ -40,7 +49,7 @@ struct Train: Vehicle, CustomStringConvertible , CustomLocalizedStringResourceCo } } -struct Bike: Vehicle, CustomStringConvertible { +struct Bike: @MainActor Vehicle, CustomStringConvertible { var name: String var price: Double var color: Color diff --git a/RangeView.swift b/RangeView.swift new file mode 100644 index 0000000..20c7c83 --- /dev/null +++ b/RangeView.swift @@ -0,0 +1,92 @@ +import SwiftUI + +import Playgrounds +import Foundation + +struct RangeView: TabContent { + init() { + texte = [] + } + + static var title = "Ranges" + static var image = "list.bullet" + + @State var texte: [String] + + var body: some View { + List(2...16, id: \.self) { i in + HStack(spacing:0){ + Text("\(i)") + if i.isMultiple(of: 3) { Text("Fizz")} + + if i.isMultiple(of: 5) { Text("Buzz")} + } + } + + + } +} + + +#Playground { + var students = ["Bill", "Linus"] + let size = [1.65, 1.79] + + func calendartest(){ + let born = 1973 + let calendar = Calendar.current + let components = calendar.dateComponents([.year], from: Date()) + if let today = components.year + { + for year in 2020...2035 { + if today > year { + print ("\(year) war ich \(year-born) Jahre alt") + } + else if today < year { + print ("\(year) werde ich \(year-born) Jahre alt") + + } else { + print ("\(year) bin ich \(year-born) Jahre alt") + } + } + } + } + func whilecount(){ + var countdown = Int.random(in: 1...100) + while countdown < 100 { + countdown+=1 + print ("Bin bei \(countdown)") + } + print ("done: CD = \(countdown)") + } + + func while_test(){ + var r = 0 + var c = 0 + while r != 100 { + r = Int.random(in: 1...100) + c+=1 + print ("würfel mit W100: \(r)") + } + print ("done: hab nach \(c) Versuchen \(r) gewürfelt") + } + func fizzbuzz(){ + var i = 1 + while i < 16 { + if i.isMultiple(of: 3){ + print( "Fizz")} + if i.isMultiple(of: 5){ + print( "Buzz")} + else{ + print (i) + } + } + } + + fizzbuzz() +} + + +#Preview { + RangeView() +} diff --git a/TheSwiftWeek.xcodeproj/project.pbxproj b/TheSwiftWeek.xcodeproj/project.pbxproj index 8357cb1..0297a80 100644 --- a/TheSwiftWeek.xcodeproj/project.pbxproj +++ b/TheSwiftWeek.xcodeproj/project.pbxproj @@ -10,6 +10,7 @@ 0CC14A872E92EC7B00271E8D /* App.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CC14A842E92EC7A00271E8D /* App.swift */; }; 0CC14A892E92EEA900271E8D /* Playground.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CC14A882E92EEA900271E8D /* Playground.swift */; }; FB2F07E92EA7CB25002BD499 /* ListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB2F07E82EA7CB25002BD499 /* ListView.swift */; }; + FB79FE462EA8CFD20011678F /* RangeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB79FE452EA8CFD20011678F /* RangeView.swift */; }; FBA00D6D2EA78411006F8B9A /* Localizable.xcstrings in Resources */ = {isa = PBXBuildFile; fileRef = FBA00D6C2EA78411006F8B9A /* Localizable.xcstrings */; }; FBA00D6F2EA78853006F8B9A /* Color.swift in Sources */ = {isa = PBXBuildFile; fileRef = FBA00D6E2EA78850006F8B9A /* Color.swift */; }; FBA00D712EA7A839006F8B9A /* PaddingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FBA00D702EA7A830006F8B9A /* PaddingView.swift */; }; @@ -28,6 +29,7 @@ 0CC14A842E92EC7A00271E8D /* App.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = App.swift; sourceTree = ""; }; 0CC14A882E92EEA900271E8D /* Playground.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Playground.swift; sourceTree = ""; }; FB2F07E82EA7CB25002BD499 /* ListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ListView.swift; sourceTree = ""; }; + FB79FE452EA8CFD20011678F /* RangeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RangeView.swift; sourceTree = ""; }; FBA00D6C2EA78411006F8B9A /* Localizable.xcstrings */ = {isa = PBXFileReference; lastKnownFileType = text.json.xcstrings; path = Localizable.xcstrings; sourceTree = ""; }; FBA00D6E2EA78850006F8B9A /* Color.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Color.swift; sourceTree = ""; }; FBA00D702EA7A830006F8B9A /* PaddingView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaddingView.swift; sourceTree = ""; }; @@ -67,6 +69,7 @@ FBA6FA612EA76AAD00C373EC /* BikeView.swift */, FBA6FA652EA7725A00C373EC /* TrainView.swift */, FB2F07E82EA7CB25002BD499 /* ListView.swift */, + FB79FE452EA8CFD20011678F /* RangeView.swift */, FBA6FA5D2EA63EA300C373EC /* Models.swift */, FBA00D6C2EA78411006F8B9A /* Localizable.xcstrings */, 0CC14A772E92EC4700271E8D /* Products */, @@ -157,6 +160,7 @@ buildActionMask = 2147483647; files = ( FBA6FA5E2EA63EA300C373EC /* Models.swift in Sources */, + FB79FE462EA8CFD20011678F /* RangeView.swift in Sources */, FBA6FA662EA7725A00C373EC /* TrainView.swift in Sources */, FBA00D752EA7ACE1006F8B9A /* SwapperView.swift in Sources */, 0CC14A892E92EEA900271E8D /* Playground.swift in Sources */, diff --git a/TheSwiftWeek.xcodeproj/project.xcworkspace/xcuserdata/rohing73.xcuserdatad/UserInterfaceState.xcuserstate b/TheSwiftWeek.xcodeproj/project.xcworkspace/xcuserdata/rohing73.xcuserdatad/UserInterfaceState.xcuserstate index ddf4887..7c2dac6 100644 Binary files a/TheSwiftWeek.xcodeproj/project.xcworkspace/xcuserdata/rohing73.xcuserdatad/UserInterfaceState.xcuserstate and b/TheSwiftWeek.xcodeproj/project.xcworkspace/xcuserdata/rohing73.xcuserdatad/UserInterfaceState.xcuserstate differ