mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 17:41:41 +00:00
- update examples
- add examples for labeled tuples and computed properties
This commit is contained in:
parent
3a7e00127f
commit
afc5ea1465
@ -224,6 +224,16 @@ let (_, price1, _) = pricesTuple // price1 == 3.69
|
|||||||
println(price1 == pricesTuple.1) // true
|
println(price1 == pricesTuple.1) // true
|
||||||
println("Gas price: \(price)")
|
println("Gas price: \(price)")
|
||||||
|
|
||||||
|
// Named tuple params
|
||||||
|
func getGasPrices2() -> (lowestPrice: Double, highestPrice: Double, midPrice: Double) {
|
||||||
|
return (1.77, 37.70, 7.37)
|
||||||
|
}
|
||||||
|
let pricesTuple2 = getGasPrices2()
|
||||||
|
let price2 = pricesTuple2.lowestPrice
|
||||||
|
let (_, price3, _) = pricesTuple2
|
||||||
|
println(pricesTuple2.highestPrice == pricesTuple2.1) // true
|
||||||
|
println("Highest gas price: \(pricesTuple2.highestPrice)")
|
||||||
|
|
||||||
// Variadic Args
|
// Variadic Args
|
||||||
func setup(numbers: Int...) {
|
func setup(numbers: Int...) {
|
||||||
// its an array
|
// its an array
|
||||||
@ -337,6 +347,11 @@ internal class Rect: Shape {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Computed properties must be declared as `var`, you know, cause they can change
|
||||||
|
var smallestSideLength: Int {
|
||||||
|
return self.sideLength - 1
|
||||||
|
}
|
||||||
|
|
||||||
// Lazily load a property
|
// Lazily load a property
|
||||||
// subShape remains nil (uninitialized) until getter called
|
// subShape remains nil (uninitialized) until getter called
|
||||||
lazy var subShape = Rect(sideLength: 4)
|
lazy var subShape = Rect(sideLength: 4)
|
||||||
|
Loading…
Reference in New Issue
Block a user