RangeView Fizz Buzz beispiel

This commit is contained in:
Ingo Rohlf 2025-10-22 13:24:48 +02:00
parent 7aebdcefb1
commit 76e74d6729
9 changed files with 172 additions and 51 deletions

View file

@ -11,7 +11,7 @@ struct TheSwiftWeek: App {
VStack{
CountView()
ContentView()
}
}

View file

@ -17,9 +17,9 @@ enum TabLabel: String {
} }
}
func tab<C: TabContent>(_ contentType: C.Type)
func tab<C: TabContent>(_ contentType: C.Type )
-> Tab<Never, C, DefaultTabLabel> {
Tab(C.title, systemImage: C.image) {
Tab(C.title, systemImage: C.image ) {
C()
}
}
@ -27,24 +27,28 @@ func tab<C: TabContent>(_ 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)
}
}

View file

@ -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()
}

View file

@ -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))
}
}

View file

@ -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" : {

View file

@ -2,18 +2,27 @@
import Foundation
import SwiftUI
protocol Vehicle {
protocol Vehicle: Identifiable {
var name: String { get }
var color: Color { get set }
}
extension Vehicle {
var id: UUID {UUID()}
}
//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: Vehicle, CustomStringConvertible , CustomLocalizedStringResourceConvertible {
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

92
RangeView.swift Normal file
View file

@ -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()
}

View file

@ -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 = "<group>"; };
0CC14A882E92EEA900271E8D /* Playground.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Playground.swift; sourceTree = "<group>"; };
FB2F07E82EA7CB25002BD499 /* ListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ListView.swift; sourceTree = "<group>"; };
FB79FE452EA8CFD20011678F /* RangeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RangeView.swift; 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>"; };
FBA00D702EA7A830006F8B9A /* PaddingView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaddingView.swift; sourceTree = "<group>"; };
@ -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 */,