Schulung_iOS/Playground.swift
2025-10-20 14:48:30 +02:00

67 lines
1.4 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
}
}
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 bike called \(name) and it is \(color)." }}
static let trek = Bike(name: "Trek", color: "red" )
}
#Playground {
// var greeting = "Hallo Playground"
var bike = Bike.trek
var train = Train(name:"ICE")
// bike.description
// bike.description = "Mein Rad"
// bike.description
train
train.delayed(minutes: 23)
BikeGarage().park(vehicle: bike)
print( bike)
// write code here
}