| 1 | <h2>Comments</h2> |
| 2 | <pre><code>// this is a comment |
| 3 | /* this is also a comment, |
| 4 | but written over multiple lines */ |
| 5 | </code></pre> |
| 6 | |
| 7 | <h2>Numbers</h2> |
| 8 | <pre><code>42 |
| 9 | -23 |
| 10 | 3.14159 |
| 11 | 0.1 |
| 12 | -273.15 |
| 13 | 1.25e-2 |
| 14 | 0xC.3p0 |
| 15 | 1_000_000 |
| 16 | 1_000_000.000_000_1</code></pre> |
| 17 | |
| 18 | <h2>Strings</h2> |
| 19 | <pre><code>let someString = "Some string literal value" |
| 20 | var emptyString = "" |
| 21 | // String interpolation |
| 22 | let multiplier = 3 |
| 23 | "\(multiplier) times 2.5 is \(Double(multiplier) * 2.5)"</code></pre> |
| 24 | |
| 25 | <h2>Control flow</h2> |
| 26 | <pre><code>for index in 1...5 { |
| 27 | println("\(index) times 5 is \(index * 5)") |
| 28 | } |
| 29 | for _ in 1...power { |
| 30 | answer *= base |
| 31 | } |
| 32 | while square < finalSquare { |
| 33 | // roll the dice |
| 34 | if ++diceRoll == 7 { diceRoll = 1 } |
| 35 | // move by the rolled amount |
| 36 | square += diceRoll |
| 37 | if square < board.count { |
| 38 | // if we're still on the board, move up or down for a snake or a ladder |
| 39 | square += board[square] |
| 40 | } |
| 41 | } |
| 42 | switch someCharacter { |
| 43 | case "a", "e", "i", "o", "u": |
| 44 | println("\(someCharacter) is a vowel") |
| 45 | case "b", "c", "d", "f", "g", "h", "j", "k", "l", "m", |
| 46 | "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z": |
| 47 | println("\(someCharacter) is a consonant") |
| 48 | default: |
| 49 | println("\(someCharacter) is not a vowel or a consonant") |
| 50 | } |
| 51 | </code></pre> |
| 52 | |
| 53 | <h2>Classes and attributes</h2> |
| 54 | <pre><code>class MyViewController: UIViewController { |
| 55 | @IBOutlet weak var button: UIButton! |
| 56 | @IBOutlet var textFields: [UITextField]! |
| 57 | @IBAction func buttonTapped(AnyObject) { |
| 58 | println("button tapped!") |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | @IBDesignable |
| 63 | class MyCustomView: UIView { |
| 64 | @IBInspectable var textColor: UIColor |
| 65 | @IBInspectable var iconHeight: CGFloat |
| 66 | /* ... */ |
| 67 | }</code></pre> |