Skip to content

Instantly share code, notes, and snippets.

@fisherds
fisherds / SwiftOptionals.swift
Last active August 29, 2015 14:11
Code used in our Swift Playgrounds lecture
import UIKit
var x = 7
//var x : Float = 7
let dave = "Dave"
//dave = "Bob"
// Optionals
var optionalFloat : Float?
var requiredFloat : Float
@fisherds
fisherds / SwiftStrings.swift
Last active April 3, 2020 14:35
Code used in our Swift Playgrounds lecture
import Foundation
// String Concatenation
var myStr = "Hello," + " World"
// String append
myStr += "!"
// Looping over a string
for char in myStr {
@fisherds
fisherds / SwiftCollections.swift
Last active March 1, 2018 14:12
Code used in our Swift Playgrounds lecture
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!")
}
@fisherds
fisherds / SwiftFunctions.swift
Created December 9, 2014 03:28
Code used in our Swift Playgrounds lecture
// Basic function
func stringRepeater(originalString : String, repeatCount : Int) -> String {
var repeatedString = ""
for _ in 0..<repeatCount {
repeatedString += originalString
}
return repeatedString
}
stringRepeater("Ho! ", 3)
@fisherds
fisherds / SwiftEnums.swift
Last active August 29, 2015 14:11
Code used in our Swift Playgrounds lecture
// Basic enum
enum Weekday {
case Monday, Tuesday, Wednesday, Thursday, Friday
}
var today : Weekday
today = .Tuesday
switch today {
case .Monday, .Tuesday, .Thursday:
@fisherds
fisherds / SwiftClasses.swift
Last active August 29, 2015 14:11
Code used in our Swift Playgrounds lecture
// Simple class
class BankAccount {
var name : String
var balance : Double
init(name : String, balance : Double) {
self.name = name
self.balance = balance
}
@fisherds
fisherds / SwiftClosures.swift
Created December 11, 2014 17:26
Code used in our Swift Playgrounds lecture
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
@fisherds
fisherds / SwiftOptionalsPart1.swift
Last active February 28, 2018 19:18
Code used in our Swift Playgrounds lecture
// 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
@fisherds
fisherds / SwiftOptionalsPart2.swift
Last active February 28, 2018 19:33
Code used in our Swift Playgrounds lecture
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
@fisherds
fisherds / SwiftEnumsPart1.swift
Last active March 1, 2018 14:54
Code used in our Swift Playgrounds lecture
// 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: