This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import UIKit | |
| var x = 7 | |
| //var x : Float = 7 | |
| let dave = "Dave" | |
| //dave = "Bob" | |
| // Optionals | |
| var optionalFloat : Float? | |
| var requiredFloat : Float |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Foundation | |
| // String Concatenation | |
| var myStr = "Hello," + " World" | |
| // String append | |
| myStr += "!" | |
| // Looping over a string | |
| for char in myStr { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import UIKit | |
| // Basics for arrays | |
| var names = ["Dave", "Kristy", "McKinley", "Keegan", "Bowen", "Neala"] | |
| names[1] | |
| names[2] = "Kingsley" | |
| names | |
| if names.contains("Dave") { | |
| print("Dave is present!") | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Basic function | |
| func stringRepeater(originalString : String, repeatCount : Int) -> String { | |
| var repeatedString = "" | |
| for _ in 0..<repeatCount { | |
| repeatedString += originalString | |
| } | |
| return repeatedString | |
| } | |
| stringRepeater("Ho! ", 3) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Basic enum | |
| enum Weekday { | |
| case Monday, Tuesday, Wednesday, Thursday, Friday | |
| } | |
| var today : Weekday | |
| today = .Tuesday | |
| switch today { | |
| case .Monday, .Tuesday, .Thursday: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Simple class | |
| class BankAccount { | |
| var name : String | |
| var balance : Double | |
| init(name : String, balance : Double) { | |
| self.name = name | |
| self.balance = balance | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var values = [10, 5, 2, 8, 3, 6, 1, 1000] | |
| // Verbose Closure | |
| var numValuesOver5 = values.reduce(0, combine: {(runningTotal : Int, currentValue : Int) -> Int in | |
| return currentValue > 5 ? runningTotal + 1 : runningTotal | |
| }) | |
| // Closure parameter name shorthand and trailing closure | |
| var numValuesOver9 = values.reduce(0) { | |
| return $1 > 9 ? $0 + 1 : $0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // We already know about constants vs variables but there is another choice always present. | |
| var x = 7 | |
| var f : Float = 7 | |
| let dave = "Dave" | |
| //dave = "Bob" // Would cause an error | |
| // Optionals | |
| var optionalFloat: Float? | |
| var requiredFloat: Float | |
| print("optionalFloat = \(optionalFloat)") // Generates a warning |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import UIKit | |
| let dave = "Dave" // Earlier | |
| var ten = "10" // Earlier | |
| // Views in a Playground + Optional Chaining | |
| var b = UIButton(frame: CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: 100, height: 100))) | |
| b.setTitle("Press me", for: .normal) // Try commenting out this line to see the result below | |
| b.backgroundColor = UIColor.red | |
| b.titleLabel?.text // Optional chaining | |
| b.titleLabel!.text! // Forced unwrapping |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Basic enum | |
| enum Weekday { | |
| case monday, tuesday, wednesday, thursday, friday | |
| } | |
| var today: Weekday | |
| today = .thursday | |
| //today.rawValue // Enum was not given a rawValue Type (see State below) | |
| switch today { | |
| case .monday, .tuesday, .thursday: |