swift class, func Advanced
func
omit variable name
func testA(var str: String, _ str2: String, _ str3: String) { print(str) print(str2) } testA("s", "b", "c")
variable arguments
func varArgs(sep: String = "/", strings: String...) -> String { var res = "" for str in strings { res += str + sep } return res } var total = varArgs("/", strings: "A", "B", "C") print(total)
inout reference
func inoutTest(inout array : [Int]) -> Void { array.append(1) array.append(2) array.append(3) } var array1 : [Int] = [] inoutTest(&array1) print(array1)
closure
// Closure var x = 5 let calc = {(i: Int) -> Int in return 3 * i * x } print(calc(1))
struct
// Structure struct TestStruct { var title = String() } var test = TestStruct(title: "Wow!") test.title = "Wonder" print(test.title) struct Service { var number : Int }
class
willSet, didSet
before, after to set parameters
// willSet, didSet class SetObClass { init() { title = "Ore" } var title: String { willSet { print("Call willSet old: \(title)") print("Call willSet new: \(newValue)") } didSet { print("Call didSet old: \(oldValue)") print("Call willSet new: \(title)") } } } var obj = SetObClass() obj.title = "Sunday"
deinit
class SampleClass { init() { } deinit { print("deinit") } } var sample : SampleClass? = SampleClass() sample = nil