Welcome to 2014, the year of the Swift! In this talk, Paul is going to take you through the 10 most interesting Swift topics, and convince you (if that's even needed) why Swift if the language to learn this year. We'll be using Playgrounds and even attempt to create a small app so get your laptops ready and follow along
32. Module 06 Slide 3 2 Hello24 Ltd. (c) 2014
Classes vs. Structures
struct Point {
var x = 0
var y = 0
static var counter = 0
static let origin = Point()
var description: String {
get {
switch (point.x, point.y) {
case (let 0, 0):
return "origin"
case (let x, 0):
return "on the y axis - x = (case (0, let y):
return "on the x axis - y = (default:
return "(point.x)x(point.y)"
}
}
}
func distanceToOrigin() -> Double {
return …
}
}
class Fruit {
var color: String
var diameter: Int
var area: Double {
get {
return self.width * self.height
}
set(newValue) {
self.width = sqrt(newValue)
self.height = sqrt(newValue)
}
}
init(color: String, diameter: Int) {
self.color = color
self.diameter = diameter
}
func area() -> Double {
return self.width * self.height
}
}
33. Module 06 Slide 3 3 Hello24 Ltd. (c) 2014
Classes vs. Structures
Classes Structs
passed by reference value
inheritance ✔ ✘
initializers must be defined auto-generated
member-wise initialiser
deinitializer ✔ ✘
introspection ✔ ✘
ideal for complex data relatively simple data *
34. Module 05 Slide 3 4 Hello24 Ltd. (c) 2014
Value type (Structures)
var point1 = Point(x: 100, y: 200)
var point2 = point1
x: 100
point1.x = 120
point1 y : 200
x: 100
point2 y : 200
35. Module 05 Slide 3 5 Hello24 Ltd. (c) 2014
Value type (Structures)
var point1 = Point(x: 100, y: 200)
var point2 = point1
x: 120
point1.x = 120
point1 y : 200
x: 100
point2 y : 200
41. ❗️
Module 02 Slide 4 1 Hello24 Ltd. (c) 2014
Optionals
var answer: Int = 42
var theAnswer: Int = nil
var theAnswer: Int? ✔
theAnswer = 42
nil
{Some 42}
42. Optionals == Schrödinger’s cat
Can either:
‣ be nil
‣ contain a value
Module 02 Slide 4 2 Hello24 Ltd. (c) 2014
43. Module 02 Slide 4 3 Hello24 Ltd. (c) 2014
Using an optional
var theAnswer: Int?
if theAnswer != nil {
var x = 4 + theAnswer
}
❗️
44. Module 02 Slide 4 4 Hello24 Ltd. (c) 2014
Forced Unwrapping
var theAnswer: Int?
if theAnswer != nil {
var x = 4 + theAnswer!
}
✔
45. Module 02 Slide 4 5 Hello24 Ltd. (c) 2014
Optional binding
if theAnswer != nil {
if let answer = theAnswer {
var x = 4 + answer
}
var x = 4 + theAnswer!
}
no !
46. Module 02 Slide 4 6 Hello24 Ltd. (c) 2014
Optionals
var theAnswer: Int?
if theAnswer != nil {
var x = 4 + theAnswer!
}
theAnswer!
47. Module 02 Slide 4 7 Hello24 Ltd. (c) 2014
Optionals
var theAnswer = 42
var theAnswer: Int?
var theAnswer: Int!
- always has a value
- type can be inferred
- either nil or has a value
- must be unwrapped
- nil until first assignment
- assumed to always have a value
48. Nil coalescing operator
Module 02 Slide 4 8 Hello24 Ltd. (c) 2014
var theAnswer: Int?
var x = theAnswer ?? 12
equivalent to:
var x = (theAnswer != nil) ? theAnswer! : 12
61. Module 04 Slide Hello24 Ltd. (c) 2014
• global function
• have name & don’t capture environment
• nested function
• have name & capture environment
• closure expressions
• no name & can capture environment
61
Types of Closures
86. Slide 8 6 Hello24 Ltd. (c) 2014
REPL
Read–eval–print loop
$ which swift
/usr/bin/swift
$ swift -version
Swift version 1.1 (swift-600.0.54.20)
Target: x86_64-apple-darwin14.0.0
89. $ swift
Welcome to Swift! Type :help for assistance.
1> 1 + 2
$R0: Int = 3
2> "once upon a time"
$R1: String = "once upon a time"
3> $R1 + " there were ($R0) bears"
$R2: String = "once upon a time there were 3 bears"
4> println($R2)
once upon a time there were 3 bears
Slide Hello24 Ltd. (c) 2014
89
REPL
90. ‣ iOS 8 released yesterday
‣ Swift is v.1.0 as of Sept 9th
‣ Xcode 6.0.1 released yesterday
‣ Apps written in Swift started being accepted on Sept 9th
Slide Hello24 Ltd. (c) 2014
90
Resources
91. ‣ iOS 8 released yesterday
‣ Swift is v.1.0 as of Sept 9th
‣ Xcode 6.0.1 released yesterday
‣ Apps written in Swift started being accepted on Sept 9th
Slide Hello24 Ltd. (c) 2014
91
Resources
92. 10 reasons you’ll love Swift
6.Functions - 1st class citizens
7.Magic
8.Unicode
9.Playgrounds
10.REPL
Slide Hello24 Ltd. (c) 2014
1.No C
2.Modern
3.Safety
4.Optionals
5.Mix & match
92