= getPerson(); person.name; // Objective-C Person* person; person.name; // no Exception when access nil // Java and C++ public Person getPerson () { ! Person person = fetchFromServer(); person = validate(person) person = checkCreditHistory(person) return person //person is nil }
= getPerson(); person.name; // NullPointerException // Objective-C Person* person; person.name; // no Exception when access nil // Java and C++ public Person getPerson () { ! Person person = fetchFromServer(); person = validate(person) person = checkCreditHistory(person) return person //person is nil }
= getPerson(); if(person != null) { person.name; } // Objective-C Person* person; person.name; // no Exception when access nil // Java and C++ public Person getPerson () { ! Person person = fetchFromServer(); person = validate(person) person = checkCreditHistory(person) return person //person is nil }
= getPerson(); if(person != null) { person.name; } // Objective-C Person* person; if(person) { NSString *name = person.name; if(name) { //Use name } } // Java and C++ public Person getPerson () { ! Person person = fetchFromServer(); person = validate(person) person = checkCreditHistory(person) return person //person is nil }
= getPerson(); if(person != null) { person.name; } // Objective-C Person* person; if(person) { NSString *name = person.name; if(name) { //Use name } } // Java and C++ public Person getPerson () { ! Person person = fetchFromServer(); person = validate(person) person = checkCreditHistory(person) return person //person is nil } nil for Scalar value
= getPerson(); if(person != null) { person.name; } // Objective-C Person* person; if(person) { NSString *name = person.name; if(name) { //Use name } } // Java and C++ public Person getPerson () { ! Person person = fetchFromServer(); person = validate(person) person = checkCreditHistory(person) return person //person is nil } nil for Scalar value 0 -1 NSNotFound IntMax NULL
! var person = getPerson() person.name ! ! var maybePerson: Person? ! if maybePerson != nil { maybePerson!.name //unwrap optional with ! } ! if let actualPerson = maybePerson { actualPerson.name }
! var person = getPerson() person.name ! ! var maybePerson: Person? ! if maybePerson != nil { maybePerson!.name //unwrap optional with ! } ! if let actualPerson = maybePerson { actualPerson.name } ! var person: Person? if let person = person { person.name }
Decrease every value in array func increase(ar :[Int], #by: Int) -> [Int] { ! var result = ar for var i = 0; i < ar.count; i++ { result[i] += by } return result }
Decrease every value in array func increase(ar :[Int], #by: Int) -> [Int] { ! var result = ar for var i = 0; i < ar.count; i++ { result[i] += by } return result } ! func decrease(ar :[Int], #by: Int) -> [Int] { var result = ar for var i = 0; i < ar.count; i++ { result[i] -= by } return result }
= decrease(ar, by:5) // [6, 7, 8] func increase(ar :[Int], #by: Int) -> [Int] { ! var result = ar for var i = 0; i < ar.count; i++ { result[i] += by } return result } ! func decrease(ar :[Int], #by: Int) -> [Int] { var result = ar for var i = 0; i < ar.count; i++ { result[i] -= by } return result }
= ar for var i = 0; i < ar.count; i++ { result[i] -= by } return result } Functional func increase(ar :[Int], #by: Int) -> [Int] { ! var result = ar for var i = 0; i < ar.count; i++ { result[i] += by } return result }
= ar for var i = 0; i < ar.count; i++ { result[i] -= by } return result } Functional func increase(ar :[Int], #by: Int) -> [Int] { ! var result = ar for var i = 0; i < ar.count; i++ { result[i] += by } return result } it’s start to smell like a Shit
func modify(ar: [Int], operation: (Int) ->(Int) ) -> [Int] { ! var result = ar for var i = 0; i < ar.count; i++ { // Do operation result[i] = operation(ar[i]) } return result } ! ! func plusOne(a: Int ) -> Int { return a + 1 }
+ 1 } ar = modify(ar) { $0 - 2 } ar = modify(ar) { $0 * 2 } func modify(ar: [Int], operation: (Int) ->(Int) ) -> [Int] { ! var result = ar for var i = 0; i < ar.count; i++ { // Do operation result[i] = operation(ar[i]) } return result }
• After memory is fully initialised can access self • Subclasses initialisers must delegate up (call super.init) • Designated and Connivance initialisers • Not inherited by default
swapInt(&a, &b) ! var str1 = "One" var str2 = “Two" func swapInt(inout a: Int, inout b: Int) { let tmp = a a = b b = tmp } ! func swapSting(inout a: String, inout b: String) { let tmp = a a = b b = tmp }
swapInt(&a, &b) ! var str1 = "One" var str2 = “Two” ! swapSting(&str1, &str2) func swapInt(inout a: Int, inout b: Int) { let tmp = a a = b b = tmp } ! func swapSting(inout a: String, inout b: String) { let tmp = a a = b b = tmp }
swapInt(&a, &b) ! var str1 = "One" var str2 = “Two” ! swapSting(&str1, &str2) func swapInt(inout a: Int, inout b: Int) { let tmp = a a = b b = tmp } ! func swapSting(inout a: String, inout b: String) { let tmp = a a = b b = tmp }
swapInt(&a, &b) ! var str1 = "One" var str2 = “Two” ! swapSting(&str1, &str2) func swapInt(inout a: Int, inout b: Int) { let tmp = a a = b b = tmp } ! func swapSting(inout a: String, inout b: String) { let tmp = a a = b b = tmp } Do you feel this smell ??
{ return ("data from server", nil) } ! let res = getServerContent() ! res.error res.data ! if let error = res.error { println("Error \(error.description) with code: \(error.errorCode)") }
{ ! case (0, 0): println("(0, 0) rect”) ! case let (width, height) where width == height: println("square with sides \(width)”) ! case (1...10, 1...10): println("small rectangle”) ! case let (width, height): println("rectangle with width \(width) and height \(height)") }
in swift • Automatically generated interface headers • All standard frameworks • All custom libraries (All the goodies from Github) • 80 % Swift code available in swift Swift features (Tuples, Optionals) is not available • Mark Swift class as Objective-C compatible @objc