Swift is a new programming language that is statically typed, multi-paradigm, and designed to work with Cocoa and Cocoa Touch. It features modern language elements like optionals, generics, closures, and pattern matching. Swift code is compiled to native executables making it performant, and it is designed to bridge seamlessly with Objective-C and Cocoa allowing existing iOS and OS X apps to migrate code over.
11. Optionals
• Can be safely unwrapped (if/else)
• Can be force unwrapped (runtime exception if
value is missing)
12. Classes, structs,
enumerations
• Classes passed by reference, structs - by value
• Using a struct has no runtime penalty
• All scalars and even Bool are structs
• Enumerations are extremely powerful
19. Enumerations
enum Tree<T> {
case …
func depth() -> Int {
func _depth<T>(t: Tree<T>) -> Int {
switch t {
case .Empty:
return 0
}
return _depth(self)
}
}
20. Enumerations
enum Tree<T> {
case …
func depth() -> Int {
func _depth<T>(t: Tree<T>) -> Int {
switch t {
case .Empty:
return 0
case .Leaf(let _):
return 1
}
return _depth(self)
}
}
21. Enumerations
enum Tree<T> {
case …
func depth() -> Int {
func _depth<T>(t: Tree<T>) -> Int {
switch t {
case .Empty:
return 0
case .Leaf(let _):
return 1
case .Node(let lhs, let rhs):
return max(_depth(lhs), _depth(rhs))
}
}
return _depth(self)
}
}
22. Enumerations
enum Tree<T> {
case Empty
case Leaf(T)
case Node(Tree, Tree)
func depth() -> Int {
func _depth<T>(t: Tree<T>) -> Int {
switch t {
case .Empty:
return 0
case .Leaf(let _):
return 1
case .Node(let lhs, let rhs):
return max(_depth(lhs), _depth(rhs))
}
}
return _depth(self)
}
}
23. Collections
• Array, Dictionary, String (contains Char)
• Collections are structs
• Implicitly bridged to Cocoa collection types
24. Collections
func filter<S : Sequence>(…) -> Bool) ->
FilterSequenceView<S>
func reverse<C : Collection …>(source: C) ->
ReverseView<C>
Some of operations are lazy evaluated
25. Collections
func filter<S : Sequence>(…) -> Bool) ->
FilterSequenceView<S>
func reverse<C : Collection …>(source: C) ->
ReverseView<C>
Some of operations are lazy evaluated
31. 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
}
32. What Swift is missing
• Preprocessor
• Exceptions
• Access control *
• KVO, KVC
• Compiler attributes (platforms, deprecations, etc.)
• performSelector: is unavailable
33. 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
39. Objective-C bridging
• All object types are mapped as implicitly
unwrapped optionals (T!)
• All ‘id’ types are mapped as ‘AnyObject’
40. Swift internals
• Swift objects are Obj-c objects
• Implicit root class ‘SwiftObject’
• Ivars type encoding is stored separately
• Method’s vtable
• Name mangling
41. Name mangling
class Foo {
func bar() -> Bool {
return false
}
}
_TFC9test3Foo3barfS0_FT_Sb
Swift keeps function metadata encoded in function symbols
42. Performance
• 10-100 x slower than C++ (-O0)
• 10 x slower than C++ (-O3)
• 1 x as C++ (-Ofast)*
47. 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.")
}
48. 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
49. 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.")
50. Auto closures
Wraps function argument in explicit closure
func assert(condition: @auto_closure () -> Bool,
message: String) {
#if DEBUG
if !condition() { println(message) }
#endif
}
assert(5 % 2 == 0, "5 isn't an even number.")
51. Implicit type conversion
struct Box<T> {
let _value : T
init (_ value: T) {
_value = value
}
}
let boxedInt = Box(1) //Box<Int>
54. Implicit type conversion
• allows any type to be ‘nil’ (which has NilType)
• allows toll-free-bridging with Cocoa types
55. 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
56. Direct call of C functions
@asmname - allows to provide a Swift interface for C functions
@asmname("my_c_func")
func my_c_func(UInt64, CMutablePointer<UInt64>) -> CInt;
57. Scripting and REPL
• xcrun swift - launches REPL
• xcrun -i ‘file.swift’ - executes script
58. Where can I swift?
• BDD Testing framework: Quick
• Reactive programming: RXSwift
• Model mapping: Crust
• Handy JSON processing: SwiftyJSON