Schulung_iOS/Playground.swift
2025-10-20 15:51:34 +02:00

88 lines
1.9 KiB
Swift

// https://carrascomolina.com
import Playgrounds
import Foundation
protocol Vehicle {
// name ist read-only
var name: String {get}
var color: String {get set}
}
protocol Garage {
associatedtype V: Vehicle
func park(vehicle: V)
}
struct BikeGarage: Garage {
func park(vehicle: Bike){
vehicle.emptyBasket()
}
}
struct Train: Vehicle {
var name: String
var color = "white"
var delay = 0 // nicht im protocol
func delayed(minutes: Int) -> Train {
var train = self
train.delay = minutes
return train
}
let makeSomeNoise = { (noise: String) -> String in
noise.uppercased()
}
var content: () -> Vehicle
}
struct ICE: Vehicle {
var name = "ICE"
var color = "white"
}
struct Bike: Vehicle, CustomStringConvertible {
var name: String
var color: String
// newValue ist ein special keyword
func emptyBasket(){
print(description + " korb ausgekippt", Date())
}
var description: String {
set { name = newValue }
get {"A \(price)€ bike called \(name) and it is \(color)." }}
static let trek = Bike(name: "Trek", color: "red" )
var price: some Equatable {
// switch (Int.random(in: 1...3)) {
// case 1:
// Double.random(in: 1000...9000)
// case 2:
// "Free"
// default:
Int.random(in: 100...900)
// }
}
}
var bike = Bike.trek
let train = Train(name: "RadZug", content: { Bike.trek })
#Playground {
// var greeting = "Hallo Playground"
// var train = Train(name:"ICE")
// bike.description
// bike.description = "Mein Rad"
// bike.description
// train
// train.delayed(minutes: 23)
// BikeGarage().park(vehicle: bike)
// print( bike)
// train.makeSomeNoise("huhu")
// // write code here
train.content().name
}