Slide 54
Slide 54 text
Structs
struct SolarSystem {
let star: Star
var planets: [Planet]?
func description() -> String {
var description = "The \(star.name) system"
if let planets = planets {
description += ", orbited by "
for i in 0 ..< planets.count {
description += planets[i].name
if (i + 2 == planets.count) { description += ", and " }
else if (i + 1 < planets.count) { description += ", " }
}
}
return description
}
}
let ourSystem = SolarSystem(star: sol, planets: [mercury, venus, earth, mars, jupiter, saturn,
uranus, neptune])
ourSystem.description() // The Sol system, orbited by Mercury, Venus, Earth, Mars, Jupiter,
Saturn, Uranus, and Neptune