Last active
November 28, 2015 13:20
-
-
Save vikashvikram/8aea0b78035910e35356 to your computer and use it in GitHub Desktop.
This file contains 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
//: Playground - noun: a place where people can play | |
import UIKit | |
var constantString = "Hi" | |
var str: String? = "Hello, playground" | |
//str = nil | |
print(str) | |
var counter = 0 | |
for counter in 0..<10 { | |
guard counter != 2 else { continue } | |
if counter != 5 { | |
print(counter) | |
} | |
} | |
var animals = ["chickens", "cows", "ducks"] | |
animals[1] | |
animals[1] = "geese" | |
animals | |
var cuteness = [ | |
"chickens": "somewhat", | |
"geese": "scary", | |
"ducks": "cute" | |
] | |
cuteness["ducks"] | |
for animal in animals { | |
cuteness[animal] | |
} | |
func transmorgify(species: String, weight: Int) -> Int{ | |
switch species { | |
case "duck": return 0 | |
default: return -1 | |
} | |
} | |
transmorgify("duck", weight: 10) | |
let x: Int = 1 | |
if (true) { print("hello") } | |
let array: [Int] = [] | |
var maybeString: String? = nil | |
// optional binding | |
if let string = maybeString { | |
string.characters.count | |
} else { | |
0 | |
} | |
// implicitely unwrapped optionals | |
let string2: String! = "Hi" | |
//CLOSURES | |
// named function | |
func performMagic(thingy: String) -> String { | |
return thingy | |
} | |
performMagic("Hey") | |
var magicFunction = performMagic | |
magicFunction("Hey") | |
// anonymous function | |
var newMagicFunction = { | |
(thingy: String) -> String in | |
return thingy | |
} | |
newMagicFunction("Hey") | |
// scope of variables and constants for closures | |
// closure does not capture their value at the time of their definition | |
var b = 3 | |
var adderFunction: (Int) -> Int = { | |
(a: Int) -> Int in | |
return a+b | |
} | |
adderFunction(1) | |
b = 2 | |
adderFunction(1) | |
// if a function or closure is the last argument then you can put it out of () and use only {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment