Skip to content

Instantly share code, notes, and snippets.

@damouse
Created October 30, 2015 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damouse/d34a8506aad904943f8a to your computer and use it in GitHub Desktop.
Save damouse/d34a8506aad904943f8a to your computer and use it in GitHub Desktop.
//: Playground - noun: a place where people can play
import UIKit
// Basic functions
func add(a: Int, b: Int) -> Int {
return a + b
}
func subtract(a: Int, b: Int) -> Int {
return a - b
}
add(2, b: 3)
subtract(2, b: 3)
// Make a function that performs dynamic math
// Assume the two functions above are extremely complicated
func doMath(a: Int, b: Int, type: String) -> Int {
if type == "add" {
return add(a, b: b)
}
if type == "subtract" {
return subtract(a, b: b)
}
print("Unknown operation " + type)
return -1
}
doMath(2, b: 3, type: "add")
doMath(2, b: 3, type: "subtract")
// Now lets try it with closures-- what were doing is passing in our functionality
// Methods have types. This is called a first class function. Because of that, we
// can pass in a function.
// Note: sabe as a funtion but starting with the open brace. In means the closures
// code follows
let dynamicAdd = { (a: Int, b: Int) -> Int in
return a + b
}
let dynamicSubtract = { (a: Int, b: Int) -> Int in
return a + b
}
func doMathClosure(a: Int, b: Int, operation: (Int, Int) -> Int) -> Int {
return operation(a, b)
}
doMathClosure(2, b: 3, operation: dynamicAdd)
doMathClosure(2, b: 3, operation: dynamicSubtract)
//// Optionals
// Nil is the absence of value (same as null)
var x = 1
var y: Int
//println(x)
//println(y)
// This works in other languages, why not in swift? Because explicit is better
// than implicit-- you should always know if a value can be null
// Look at the add function above. What if one of the parameters is nil? Failure!
func hello(name: String) {
print("Hello " + name)
}
hello("damouse")
// Now what if we may wany to handle nil? What if something just cant exist yet?
// Information from the backend, the user's name
let userMiddleName = ""
// In an app do we test for this if the user has already signed up? Yes, there
// are ways, but they are not elegant
// Need a way to check if the name exists
// a ? indicates an optional
var userName: String?
// Optionals wrap a value that is a question!
// a ! is a forced unwrap
// a ? is a questioned unwrap
func goodbye(name: String?) -> String {
if let y = name {
return "Bye " + y
}
else {
return "Bye stranger!"
}
// Forced unwrap- runtime error here
//return "Bye " + name!
}
goodbye(userName)
userName = "damouse"
goodbye(userName)
// A better of unwrapping
class User {
var name: String
init(n: String) {
name = n
}
}
var user: User? = User(n: "damouse")
// Printing the optional itself
print(user)
print(user!.name)
// Difference between forced unwrap?
user = nil
print(user?.name) // no crash
//print(user!.name) // crash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment