Created
January 12, 2016 07:34
-
-
Save aoriani/5a8dbf67648c927a4056 to your computer and use it in GitHub Desktop.
Notes from Introduction to Swift - WWDC 2014
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
#!/usr/bin/env xcrun swift -i | |
//No, it doesn't compile | |
//Hello World | |
println("Hello Worl") | |
//Variables | |
var aInt: Int = 1 | |
//Constants | |
let myName: String = "Andre Oriani" | |
let isDeveloper: Bool = true | |
let pi: Double = 3.14 | |
let letter: Character = "A" | |
//Infered types | |
let maxSize = 3 | |
//Like python you loop on string chars | |
for character in "word" { | |
println(character) | |
} | |
//Concatenating chars | |
let a: Character = "a" | |
let b: Character = "b" | |
let ab: String = a + b | |
//String Interpolation | |
let a = 3, b = 5 | |
let mathResult = "\(a) times \(b) equal \(a * b)" | |
//Concatenating strings | |
let aString = "Part 1" | |
let theString += " Introduction" | |
//Arrays and dictonaries | |
var names = ["a", "b", "c"] | |
var ponts = ["a": 1, "b": 10, "c": 20] | |
//Loops | |
while !ready { | |
doIt() | |
} | |
for var i = 0; i < 7; i++ { | |
println(i) | |
} | |
//Closed end | |
for i in 0...6 { | |
} | |
//Open end | |
for i in 0..5 { | |
} | |
//Dictionaries | |
for (key, value) in dict { | |
} | |
//Array append | |
var inteiros = [1, 2, 3] | |
inteiros += 4 | |
//Array update | |
inteiros[0] = 0 | |
//Ranged replacement | |
inteiros [0...3] = [10, 20, 30, 40] | |
//dictionary | |
//Adding new key | |
var dict = ["a": 1, "b": 2] | |
dict["c"] = 3 | |
//Optional | |
let num:Int? = dict["invalidKey"] | |
if num == nil { | |
println("not found") | |
} else { | |
let numero = num! | |
println("num is \(numero)") | |
} | |
if num { | |
let numero = num! | |
println("num is \(numero)") | |
} | |
if let numero = num { | |
println("num is \(numero)") | |
} | |
//Conditionals | |
if a == 0 { | |
} else if b == 1 { | |
} else { | |
} | |
switch value { | |
case 0,1: | |
case 2...9: | |
default: | |
} | |
// Functions | |
func Hello(name: String = "Default Value") -> String { | |
return "Hello " + name | |
} | |
//Tuples | |
func tuples () -> (Int, String) { | |
return (200, "Success") | |
} | |
//Named values in tuple | |
func tuples2 () -> (code: Int, message: String) { | |
return (200, "Success") | |
} | |
let status = tuples2() | |
println("code: \(status.code)") | |
//Closures | |
let helloWorld: () -> () = { | |
println("helloWorld") | |
} | |
let helloWorld = { | |
println("helloWorld") | |
} | |
func repeatableTask(count: Int, task: () -> () ) { | |
for i in 0..count { | |
task() | |
} | |
} | |
repeatableTask(2, { | |
println("hi") | |
}) | |
//If the closure is the last arg there's special syntax | |
repeatableTask(2) { | |
println("hi") | |
} | |
//Classes | |
class Klazz { | |
} | |
class SubClass: SuperClass { | |
} | |
//Properties | |
class Vehicle { | |
//Stored | |
var numberOfWheels = 0 | |
//Computed r/w | |
var desc: String { | |
get { | |
return "\(numberOfWheels) whells" | |
} | |
set { | |
//Well do something | |
} | |
} | |
//Computed read only | |
var voila: String { | |
return "Voilá" | |
} | |
} | |
let car = Vehicle() | |
println(car.desc) | |
class Bike: Vehicle { | |
init() { | |
super.init() | |
numberOfWheels = 2 | |
} | |
override var desc: String { | |
return super.desc + " haha" | |
} | |
} | |
//Property Observersers | |
class ParentsCar: Car { | |
override var speed: Double { | |
willSet { | |
//newValue | |
if newValue > 65.0 { | |
println("Careful!") | |
} | |
} | |
didSet { | |
//oldValue | |
} | |
} | |
} | |
//Methods | |
class Counter { | |
var count = 0; | |
func increment() { | |
count++ | |
} | |
} | |
class Counter2 { | |
var count = 0; | |
func increment(count: Int) { | |
self.count += count | |
} | |
} | |
//struct | |
struct Point { | |
var x,y: Double | |
} | |
struct Rect { | |
var topLeft, bottomRigh: Point | |
var area: Double { | |
get { | |
return topLeft.x * bottomRigh.y | |
} | |
} | |
func area() -> Double { | |
return topLeft.x * bottomRigh.y | |
} | |
} | |
var point = Point(x:1.0, y:2.0) | |
// structs cannot inherit from other structs | |
// structs are passed by value (cpy) and class' objects by value | |
let a: StructType = StructType() // Imutable like C++ const Point a = {x:1, y:2}; | |
struct name { | |
var x, y : Int | |
mutating func offsetBy(dx:Int, dy:Int) { //close C++ mutable | |
x += dx | |
y += dy | |
} | |
} | |
// BTW, newer Swift uses print instead of println | |
//enums | |
enum Status: Int { | |
case Failure = 0, Success, Unknown | |
} | |
let success = Status.Success.toRaw() // success == 1 | |
enum SpecialChars: Character { | |
case Tab = "\t" | |
case LineFeed = "\n" | |
} | |
enum Direction { | |
case North, South, East, West | |
} | |
var direction = Direction.North | |
direction = .South | |
//Associate value | |
enum TrainStatus { | |
case OnTime | |
case Delayed(Int) | |
init() { | |
self = OnTime | |
} | |
var desc: String { | |
switch self { | |
case OnTime: | |
return "on time" | |
case Delayed(let minutes): { | |
return "delayed \(minutes) min" | |
} | |
} | |
} | |
} | |
var status = TrainStatus() | |
status = .Delayed(5) | |
//Extensions | |
extension Int { | |
func repetitions(task: () -> ()) { | |
for in in 0..self { | |
task; | |
} | |
} | |
} | |
//Say one milion times sorry | |
10000000.repetitions { | |
print("Sorry\n") | |
} | |
//Generics | |
struct Stack<T> { | |
var elements = T[]() | |
mutating func push(element: T) { | |
element.append(element) | |
} | |
mutating func pop() -> T { | |
return elements.removeLast() | |
} | |
} | |
var stack = Stack<Int>() | |
stack.push(56) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment