• NSOperation and related friends have been internally implemented using Grand Central Dispatch, the latter seems now generally preferred. • Objective-C has blocks, syntax can be learned on goshdarnblocksyntax.com (there’s a less polite version) • A lot more changed… and we have a new programming language!
with generics • The number of libraries available is still low compared to Objective-C, but is growing • Objective-C is not going anywhere soon • Syntax keeps changing (1.2 -> 2.0)
anymore • We now have 2 new types class and struct, we can pick the one that suits best our design • The code is safer with casting “as” // passed by reference class SomeClass { var foo: String init(foo: String) { self.foo = foo } } // passed by value (copy) struct SomeStruct { var bar: String init(bar: String) { self.bar = bar } }
preserves data • struct is easier to handle in a multi-thread environment • Apple suggests to use struct whenever is possible and to use class when is necessary (identity preservation or deinitializers)
make conditional checks and to throw exceptions • defer will take care to run “cleaning” operations when leaving the scope, no matter what (f.e.: closing a file) • do, try, catch have been added to make the error handling system more powerful taking advantage of the type ErrorType • Availability check easier with #available(iOS 9, *)
} do { let beer = try getAnAwesomeBeer(pub) print("I drink it \(beer)") } catch BeerError.TooMuch { print("Hmm... too much, bartender said NO!") } catch BeerError.NotAvailable { print("Let's find another place") } catch { // Catch must be exhaustive print("Ouch!") }
to write flexible, reusable functions and types that can work with any type, subject to requirements that you define. You can write code that avoids duplication and expresses its intent in a clear, abstracted manner.” — The Swift Programming Language • A generic function uses a placeholder name, like “T” instead of an actual type, this means that the compiler will check that types are matching, throwing errors if something is wrong.
a { print(i) } } func printDoubleFromArray(a: [Double]) { for d in a { print(d) } } // both functions can be rewritten with a single one func printItemsInArray<T>(a: [T]) { for item in a { print(item) } }
is a closure, we can write it as “trailing closure”. • Closures in Swift are similar to blocks in Objective-C and to lambdas in other programming languages (ed: Java 7, C#, Scala, etc…) func someFunctionWithAClosure(closure: () -> ()) { // function body goes here } someFunctionWithAClosure(){ // closure's body goes here }
use get and set to fire events • Consider binding abstraction and use frameworks like SwiftBond • Consider Functional Reactive Programming and implementations like RAC 3.0 or RxSwift
of Apple products, sooner or later it could be necessary to learn it • Learning Swift now will pay also in along term • Swift is going Open Source, we can influence its path