Created
July 20, 2014 18:31
-
-
Save siygle/ae67a8dae900c2952c3c to your computer and use it in GitHub Desktop.
intermediate swift
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 Cocoa | |
var str = "Hello, playground" | |
// optional type | |
// not sure which kind of data we will get | |
// default to nil | |
var optionalNumber: Int? | |
optionalNumber = 6 | |
// ex | |
func findIndexOfString(string: String, array: [String]) -> Int? { | |
for (index, value) in enumerate(array) { | |
if value == string { | |
return index | |
} | |
} | |
return nil | |
} | |
var neighbors = ["Alex" , "Anna", "Dave"] | |
/* | |
var index: Int? = findIndexOfString("Anna", neighbors) | |
if index { | |
println("hello, \(neighbors[index!])") | |
} | |
*/ | |
if let index = findIndexOfString("Anna", neighbors) { | |
println("hello, \(neighbors[index])") | |
} else { | |
println("must away") | |
} | |
// optional chaining | |
/* | |
addressNumber = target.residence?.address?.buildingNumber?.toInt() | |
*/ | |
// memory management | |
// automatic reference counting | |
class BowlingPin {} | |
func juggle(count: Int) { | |
var left = BowlingPin() | |
if count > 1 { | |
var right = BowlingPin() | |
right = left | |
} | |
} | |
// won't release if still have reference | |
// ex: | |
class Apartment { | |
// need to use weak | |
weak var tenant: Person? | |
} | |
class Person { | |
// need to use weak, no need to take responsible to other class life | |
weak var home: Apartment? | |
func moveIn(apt: Apartment) { | |
self.home = apt | |
apt.tenant = self | |
} | |
} | |
var renters = ["Alan": Person()] | |
var apts = [507: Apartment()] | |
renters["Alan"]!.moveIn(apts[507]!) | |
renters["Alab"] = nil | |
apts[507] = nil | |
class Customer { | |
let name: String | |
var card: CreditCard? | |
init(name: String) { | |
self.name = name | |
} | |
deinit { | |
println("\(name) is being deinitialized!") | |
} | |
} | |
// only live when depend on specific user | |
class CreditCard { | |
let number: UInt64 | |
unowned let customer: Customer | |
init(number: UInt64, customer: Customer) { | |
self.number = number | |
self.customer = customer | |
} | |
} | |
var john: Customer? | |
john = Customer(name: "Jonny") | |
john!.card = CreditCard(number: 1234_5678_9012_3456, customer: john!) | |
// strong, weak, and unowned references | |
// use strong references from owners to the objects they own | |
// use weak references among objects with independent lifetimes | |
// use unowned references from owned objects with the same lifetime | |
// Initialization | |
// Every value must be initialized before it is used | |
var message: String | |
var fakeJudge: Bool = true | |
if fakeJudge { | |
message = "Welcome to Swift!" | |
} else { | |
message = "nono" | |
} | |
println(message) | |
// struct init | |
struct Color { | |
let red = 0.0, green = 0.0, blue = 0.0 | |
} | |
var myColor = Color() | |
// class init | |
class Car { | |
var paintCar: Color | |
init(color: Color) { | |
paintCar = color | |
} | |
} | |
class RaceCar: Car { | |
var hasTurbo: Bool | |
init(color: Color, turbo: Bool) { | |
// call super init first is not safe!! | |
//super.init(color: color) | |
hasTurbo = turbo | |
super.init(color: color) | |
} | |
// convenience initializer | |
convenience init(color: Color) { | |
self.init(color: color, turbo: true) | |
} | |
/* | |
convenience init() { | |
self.init(color: Color(gray: 0.4)) | |
}*/ | |
} | |
var race1 = RaceCar(color: Color(red: 0.4, green: 0.3, blue: 0.1)) | |
// lazy properties | |
// closures | |
var clients = ["Pestov","Serream", "Peter"] | |
/* | |
clients.sort({(a: String, b: String) -> Bool in | |
return a < b | |
}) | |
*/ | |
//clients.sort({a, b in a < b})\ | |
//clients.sort({ $0 < $1}) | |
clients.sort {$0 < $1} | |
println(clients) | |
// functional programming | |
var words = ["angry","hungry","test","tokyo","taipei"] | |
println(words.filter {$0.hasSuffix("gry")} | |
.map { $0.uppercaseString } | |
.reduce("HULK") { "\($0) \($1)" }) | |
// pattern matching | |
enum TrainStatus { | |
case OnTime | |
case Delayed(Int) | |
} | |
var trainStatus = TrainStatus.OnTime | |
switch trainStatus { | |
case .OnTime: | |
println("ontime!") | |
case .Delayed(let minutes): | |
println("delayed by \(minutes) minutes") | |
} | |
// Tuple pattern | |
let color = (1.0, 1.0, 1.0, 0.4) | |
switch color { | |
case (0.0, 0.5...1.0, let blue, _): | |
println("Green and \(blue))") | |
case let (r, g, b, trans) where r == g && g == b: | |
println("Qpa!!") | |
default: | |
println("nono") | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment