RangeView Fizz Buzz beispiel
This commit is contained in:
parent
7aebdcefb1
commit
76e74d6729
9 changed files with 172 additions and 51 deletions
|
|
@ -11,7 +11,7 @@ struct TheSwiftWeek: App {
|
||||||
VStack{
|
VStack{
|
||||||
|
|
||||||
|
|
||||||
CountView()
|
ContentView()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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<Never, C, DefaultTabLabel> {
|
||||||
Tab(C.title, systemImage: C.image) {
|
Tab(C.title, systemImage: C.image ) {
|
||||||
C()
|
C()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -27,24 +27,28 @@ func tab<C: TabContent>(_ contentType: C.Type)
|
||||||
|
|
||||||
|
|
||||||
struct ContentView: View {
|
struct ContentView: View {
|
||||||
|
@AppStorage("tabSelection") var tabSelection = TabLabel.bikes
|
||||||
|
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
TabView {
|
//TabView(selection: $tabSelection) {
|
||||||
Tab(TabLabel.bikes.title, systemImage: TabLabel.bikes.image) {
|
TabView() {
|
||||||
BikeView()
|
// Tab(TabLabel.bikes.title, systemImage: TabLabel.bikes.image) {
|
||||||
}
|
// BikeView()
|
||||||
Tab(TabLabel.bikes.title, systemImage: TabLabel.trains.image) {
|
// }
|
||||||
TrainView()
|
// Tab(TabLabel.bikes.title, systemImage: TabLabel.trains.image) {
|
||||||
}
|
// TrainView()
|
||||||
Tab("drücken", systemImage: TabLabel.cars.image) {
|
// }
|
||||||
SwapperView()
|
// Tab("drücken", systemImage: TabLabel.cars.image) {
|
||||||
}
|
// SwapperView()
|
||||||
|
// }
|
||||||
tab(ListView.self)
|
tab(ListView.self)
|
||||||
tab(CountView.self)
|
tab(CountView.self)
|
||||||
tab(BikeView.self)
|
tab(BikeView.self)
|
||||||
tab(TrainView.self)
|
tab(TrainView.self)
|
||||||
tab(SwapperView.self)
|
tab(SwapperView.self)
|
||||||
tab(PaddingView.self)
|
tab(PaddingView.self)
|
||||||
}
|
}.tabViewStyle(.sidebarAdaptable)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,34 +1,34 @@
|
||||||
//
|
|
||||||
// CountView.swift
|
|
||||||
// TheSwiftWeek
|
|
||||||
//
|
|
||||||
// Created by Ingo Rohlf on 21.10.25.
|
|
||||||
//
|
|
||||||
|
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
struct CountView: TabContent {
|
struct CountView: TabContent {
|
||||||
static var title = "Count"
|
static var title = "Count"
|
||||||
static var image = "42.circle"
|
static var image = "42.circle"
|
||||||
|
|
||||||
@State var text = ""
|
@State var text = "🐼"
|
||||||
|
|
||||||
|
@FocusState var isFocused: Bool // 1
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
TextField("Enter Emoji", text: $text)
|
TextField("Enter Emoji", text: $text)
|
||||||
.padding()
|
.padding()
|
||||||
|
.focused($isFocused) // 2
|
||||||
|
.onAppear {
|
||||||
|
isFocused = true // 3
|
||||||
|
}
|
||||||
Text("""
|
Text("""
|
||||||
Zeichenzahl: \(text.count)
|
count: \(text.count)
|
||||||
Unicode: \(text.unicodeScalars.count)
|
unicode: \(text.unicodeScalars.count)
|
||||||
UTF-8 Bytes: \(text.lengthOfBytes(using: .utf8))
|
length: \(text.lengthOfBytes(using: .utf8))
|
||||||
""")
|
""")
|
||||||
|
.background(.yellow)
|
||||||
|
.multilineTextAlignment(.center)
|
||||||
|
.bold()
|
||||||
|
.fontDesign(.rounded)
|
||||||
|
.font(.largeTitle)
|
||||||
Spacer()
|
Spacer()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#Preview {
|
#Preview {
|
||||||
CountView()
|
CountView()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,14 +23,22 @@ struct ListView: TabContent{
|
||||||
}
|
}
|
||||||
List(bikes, id: \.name){ bike in
|
List(bikes, id: \.name){ bike in
|
||||||
HStack {
|
HStack {
|
||||||
|
|
||||||
Color(bike.color)
|
Color(bike.color)
|
||||||
|
//Circle().fill(style: bike.color)
|
||||||
|
|
||||||
|
|
||||||
Text(String(bike.name))
|
Text(String(bike.name))
|
||||||
|
Spacer()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
List(bikes, id: \.name){
|
List(bikes, id: \.name){
|
||||||
Text(String($0.price))
|
Text(String($0.price))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Weil Vehicle jetzt Identifiable implementiert, geht es ohne id: \.name
|
||||||
|
List(bikes){
|
||||||
|
Text(String($0.name))
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,28 +1,32 @@
|
||||||
{
|
{
|
||||||
"sourceLanguage" : "en",
|
"sourceLanguage" : "en",
|
||||||
"strings" : {
|
"strings" : {
|
||||||
" Zeichenzahl: %lld\n Unicode: %lld\n UTF-8 Bytes: %lld" : {
|
"%lld" : {
|
||||||
"comment" : "A block of text showing the number of characters, Unicode scalars, and UTF-8 bytes in the text entered by the user.",
|
"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,
|
"isCommentAutoGenerated" : true,
|
||||||
"localizations" : {
|
"localizations" : {
|
||||||
"en" : {
|
"en" : {
|
||||||
"stringUnit" : {
|
"stringUnit" : {
|
||||||
"state" : "new",
|
"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" : {
|
"Enter Emoji" : {
|
||||||
"comment" : "A label for an text field where the user can enter an emoji.",
|
"comment" : "A label for an text field where the user can enter an emoji.",
|
||||||
"isCommentAutoGenerated" : true
|
"isCommentAutoGenerated" : true
|
||||||
},
|
},
|
||||||
"Hello, World!" : {
|
"Fizz" : {
|
||||||
"comment" : "A simple text view with padding applied to it.",
|
"comment" : "A word that is displayed when the number is a multiple of 3.",
|
||||||
"isCommentAutoGenerated" : true
|
"isCommentAutoGenerated" : true
|
||||||
},
|
},
|
||||||
"My Vehicle" : {
|
"My Vehicle" : {
|
||||||
|
|
|
||||||
25
Models.swift
25
Models.swift
|
|
@ -2,18 +2,27 @@
|
||||||
import Foundation
|
import Foundation
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
protocol Vehicle {
|
protocol Vehicle: Identifiable {
|
||||||
var name: String { get }
|
var name: String { get }
|
||||||
var color: Color { get set }
|
var color: Color { get set }
|
||||||
}
|
}
|
||||||
|
extension Vehicle {
|
||||||
protocol TabContent: View {
|
var id: UUID {UUID()}
|
||||||
init()
|
|
||||||
static var title: String { get }
|
|
||||||
static var image: String { get }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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 name: String
|
||||||
var color: Color
|
var color: Color
|
||||||
var delay: Int
|
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 name: String
|
||||||
var price: Double
|
var price: Double
|
||||||
var color: Color
|
var color: Color
|
||||||
|
|
|
||||||
92
RangeView.swift
Normal file
92
RangeView.swift
Normal 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()
|
||||||
|
}
|
||||||
|
|
@ -10,6 +10,7 @@
|
||||||
0CC14A872E92EC7B00271E8D /* App.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CC14A842E92EC7A00271E8D /* App.swift */; };
|
0CC14A872E92EC7B00271E8D /* App.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CC14A842E92EC7A00271E8D /* App.swift */; };
|
||||||
0CC14A892E92EEA900271E8D /* Playground.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CC14A882E92EEA900271E8D /* Playground.swift */; };
|
0CC14A892E92EEA900271E8D /* Playground.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CC14A882E92EEA900271E8D /* Playground.swift */; };
|
||||||
FB2F07E92EA7CB25002BD499 /* ListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FB2F07E82EA7CB25002BD499 /* ListView.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 */; };
|
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 */; };
|
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>"; };
|
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>"; };
|
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>"; };
|
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>"; };
|
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>"; };
|
FBA00D702EA7A830006F8B9A /* PaddingView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaddingView.swift; sourceTree = "<group>"; };
|
||||||
|
|
@ -67,6 +69,7 @@
|
||||||
FBA6FA612EA76AAD00C373EC /* BikeView.swift */,
|
FBA6FA612EA76AAD00C373EC /* BikeView.swift */,
|
||||||
FBA6FA652EA7725A00C373EC /* TrainView.swift */,
|
FBA6FA652EA7725A00C373EC /* TrainView.swift */,
|
||||||
FB2F07E82EA7CB25002BD499 /* ListView.swift */,
|
FB2F07E82EA7CB25002BD499 /* ListView.swift */,
|
||||||
|
FB79FE452EA8CFD20011678F /* RangeView.swift */,
|
||||||
FBA6FA5D2EA63EA300C373EC /* Models.swift */,
|
FBA6FA5D2EA63EA300C373EC /* Models.swift */,
|
||||||
FBA00D6C2EA78411006F8B9A /* Localizable.xcstrings */,
|
FBA00D6C2EA78411006F8B9A /* Localizable.xcstrings */,
|
||||||
0CC14A772E92EC4700271E8D /* Products */,
|
0CC14A772E92EC4700271E8D /* Products */,
|
||||||
|
|
@ -157,6 +160,7 @@
|
||||||
buildActionMask = 2147483647;
|
buildActionMask = 2147483647;
|
||||||
files = (
|
files = (
|
||||||
FBA6FA5E2EA63EA300C373EC /* Models.swift in Sources */,
|
FBA6FA5E2EA63EA300C373EC /* Models.swift in Sources */,
|
||||||
|
FB79FE462EA8CFD20011678F /* RangeView.swift in Sources */,
|
||||||
FBA6FA662EA7725A00C373EC /* TrainView.swift in Sources */,
|
FBA6FA662EA7725A00C373EC /* TrainView.swift in Sources */,
|
||||||
FBA00D752EA7ACE1006F8B9A /* SwapperView.swift in Sources */,
|
FBA00D752EA7ACE1006F8B9A /* SwapperView.swift in Sources */,
|
||||||
0CC14A892E92EEA900271E8D /* Playground.swift in Sources */,
|
0CC14A892E92EEA900271E8D /* Playground.swift in Sources */,
|
||||||
|
|
|
||||||
Binary file not shown.
Loading…
Reference in a new issue