- mo betta examples

- add `inout` example
- better `optional` example
- Playground issue has been resolved, uncommented `protocol` example
This commit is contained in:
C. Bess 2014-10-04 12:08:23 -05:00
parent c77c92818c
commit 923a8ed99b

View File

@ -13,6 +13,9 @@ The official [Swift Programming Language](https://itunes.apple.com/us/book/swift
See also Apple's [getting started guide](https://developer.apple.com/library/prerelease/ios/referencelibrary/GettingStarted/LandingPage/index.html), which has a complete tutorial on Swift.
```swift
// import a module
import UIKit
//
// MARK: Basics
//
@ -26,7 +29,7 @@ println("Hello, world")
var myVariable = 42
let øπΩ = "value" // unicode variable names
let myConstant = 3.1415926
let π = 3.1415926
let convenience = "keyword" // contextual variable name
let weak = "keyword"; let override = "another keyword" // statements can be separated by a semi-colon
let `class` = "keyword" // backticks allow keywords to be used as variable names
@ -34,9 +37,24 @@ let explicitDouble: Double = 70
let intValue = 0007 // 7
let largeIntValue = 77_000 // 77000
let label = "some text " + String(myVariable) // Casting
let piText = "Pi = \(myConstant), Pi 2 = \(myConstant * 2)" // String interpolation
var optionalString: String? = "optional" // Can be nil
optionalString = nil
let piText = "Pi = \(π), Pi 2 = \(π * 2)" // String interpolation
var someOptionalString: String? = "optional" // Can be nil
if someOptionalString != nil {
// I am not nil
if someOptionalString!.hasPrefix("opt") {
println("has the prefix")
}
let empty = someOptionalString?.isEmpty
}
someOptionalString = nil
if let someStringConstant = someOptionalString {
// has Some value
}
// AnyObject == id
/*
Comment here
@ -84,9 +102,10 @@ for (key, value) in dict {
}
// for loop (range)
for i in -1...1 { // [-1, 0, 1]
for i in -1...shoppingList.count {
println(i)
}
shoppingList[1...2] = ["steak", "peacons"]
// use ..< to exclude the last number
// while loop
@ -141,9 +160,16 @@ greet("Bob", "Tuesday")
func getGasPrices() -> (Double, Double, Double) {
return (3.59, 3.69, 3.79)
}
let pricesTuple = getGasPrices()
let price = pricesTuple.2 // 3.79
println("Gas price: \(price)")
// Variadic Args
func setup(numbers: Int...) {}
func setup(numbers: Int...) {
// its an array
let number = numbers[0]
let argCount = numbers.count
}
// Passing and returning functions
func makeIncrementer() -> (Int -> Int) {
@ -155,6 +181,17 @@ func makeIncrementer() -> (Int -> Int) {
var increment = makeIncrementer()
increment(7)
// pass by ref
func swapTwoInts(inout a: Int, inout b: Int) {
let tempA = a
a = b
b = tempA
}
var someIntA = 7
var someIntB = 3
swapTwoInts(&someIntA, &someIntB)
println(someIntB) // 7
//
// MARK: Closures
@ -255,8 +292,9 @@ internal class Rect: Shape {
}
init(sideLength: Int) {
super.init()
self.sideLength = sideLength
// always super.init last when init custom properties
super.init()
}
func shrink() {
@ -321,7 +359,6 @@ protocol ShapeGenerator {
func buildShape() -> Shape
}
/*
// Protocols declared with @objc allow optional functions,
// which allow you to check for conformance
@objc protocol TransformShape {
@ -341,7 +378,7 @@ class MyShape: Rect {
}
}
}
*/
//
// MARK: Other
@ -372,7 +409,7 @@ extension Int {
println(7.customProperty) // "This is 7"
println(14.multiplyBy(2)) // 42
// Generics: Similar to Java. Use the `where` keyword to specify the
// Generics: Similar to Java and C#. Use the `where` keyword to specify the
// requirements of the generics.
func findIndex<T: Equatable>(array: [T], valueToFind: T) -> Int? {