13. Classes, structs, enums
• Classes passed by reference, structs - by value
• Using a struct has no runtime penalty
• All scalars and even Bool are structs
20. Extensions
struct Foo {
let value : Int
}
extension Foo : Printable {
var description : String {
get {return "Foo"}
}
}
extension Foo : Equatable {
}
func ==(lhs: Foo, rhs: Foo) -> Bool {
return lhs.value == rhs.value
}
21. What Swift is missing
• Preprocessor
• Exceptions
• Access control *
• KVO, KVC
• Compiler attributes (platforms, deprecations, etc.)
• performSelector: is unavailable
22. Objective-C bridging
• Call Obj-c from Swift
• Call Swift from Objc with limitations
• Call CoreFoundation types directly
• C++ is not allowed (should be wrapped in Objc)
• Subclassing Swift classes not allowed in Objc
32. Compiler features
• Profile-guided optimization (‘off’ by default)
• Loop vectorisation (‘on’ by default from Xcode 6)
• High-level language-specific optimizer and IR
before LLVM (‘xcrun swift t.swift -O -emit-sil')
33. Performance
• 10-100 x slower than C++ (-O0)
• 10 x slower than C++ (-O3)
• 1 x as C++ (-Ofast)*
35. Pattern matching
let point = (0, 1)
if point.0 == 0 && point.1 == 0 {
println("Point is at the origin")
} else if point.0 >= 0 && point.0 <= 1 &&
point.1 >= 0 && point.1 <= 1 {
println("I")
}
...
36. Pattern matching
let point = (0, 1)
switch point {
case (0, 0):
println("Point is at the origin")
case (0...1, 0...1):
println("I")
case (-1...0, 0...1):
println("II")
case (-1...0, -1...0):
println("III")
case(0...1, -1...0):
println("IV")
default:
println(“I don’t know.")
}
37. Function currying
func add(a: Int)(b: Int) -> Int {
return a + b
}
let foo = add(5)(b: 3) // 8
let add5 = add(5) // (Int) -> Int
let bar = add5(b: 3) // 8
38. Auto closures
• Wraps function argument in explicit closure
func assert(condition:() -> Bool, message: String) {
#if DEBUG
if !condition() { println(message) }
#endif
}
assert({5 % 2 == 0}, "5 isn't an even number.")
43. Implicit type conversion
• allows any type to be ‘nil’ (which has NilType)
• allows toll-free-bridging with Cocoa types
44. Reflection
struct Foo {
var str = "Apple"
let int = 13
func foo() { }
}
reflect(Foo()).count // 2
reflect(Foo())[0].0 // "str"
reflect(Foo())[0].1.summary // "Apple
45. Direct call of C functions
@asmname - attribute that allows to provide a Swift interface for C functions
@asmname("my_c_func")
func my_c_func(UInt64, CMutablePointer<UInt64>) -> CInt;
46. Scripting and REPL
• xcrun swift - launches REPL
• xcrun -i ‘file.swift’ - executes script
47. Where can I swift?
• BDD Testing framework: Quick
• Reactive programming: RXSwift
• Model mapping: Crust
• Handy JSON processing: SwiftyJSON