summaryrefslogtreecommitdiffhomepage
path: root/swift.html.markdown
diff options
context:
space:
mode:
authorC. Bess <cbess@company.com>2015-11-09 17:54:05 -0600
committerC. Bess <cbess@company.com>2015-11-09 17:54:05 -0600
commitafc5ea14654e0e9cd11c7ef1b672639d12418bad (patch)
tree9e162d6b192352251c5e13f85104b031b29b30d1 /swift.html.markdown
parent3a7e00127fef6f72b36f9b86083928623d3a1ab8 (diff)
- update examples
- add examples for labeled tuples and computed properties
Diffstat (limited to 'swift.html.markdown')
-rw-r--r--swift.html.markdown17
1 files changed, 16 insertions, 1 deletions
diff --git a/swift.html.markdown b/swift.html.markdown
index 0d1d2df4..5e6b76e6 100644
--- a/swift.html.markdown
+++ b/swift.html.markdown
@@ -211,7 +211,7 @@ greet("Bob", "Tuesday")
func greet2(#requiredName: String, externalParamName localParamName: String) -> String {
return "Hello \(requiredName), the day is \(localParamName)"
}
-greet2(requiredName:"John", externalParamName: "Sunday")
+greet2(requiredName: "John", externalParamName: "Sunday")
// Function that returns multiple items in a tuple
func getGasPrices() -> (Double, Double, Double) {
@@ -224,6 +224,16 @@ let (_, price1, _) = pricesTuple // price1 == 3.69
println(price1 == pricesTuple.1) // true
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
func setup(numbers: Int...) {
// 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
// subShape remains nil (uninitialized) until getter called
lazy var subShape = Rect(sideLength: 4)