SlideShare uma empresa Scribd logo
1 de 100
Baixar para ler offline
Swift 2.2 Design Patterns
COCOACONF AUSTIN
APRIL 2016
Skyline Credit: User:Argash [GFDL (http://www.gnu.org/copyleft/fdl.html) or
CC BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0)], via Wikimedia Commons
@CarlBrwn
Swift 2.2 Design Patterns
COCOACONF AUSTIN
APRIL 2016
Skyline Credit: User:Argash [GFDL (http://www.gnu.org/copyleft/fdl.html) or
CC BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0)], via Wikimedia Commons
@CarlBrwn
[:]?
So, who is this idiot, anyway?
INTRODUCTION
@CarlBrwn
These mean a lot of different things to
different people.
Hopefully they’re of some use to you.
WIDELY RECOGNIZED,
BUT NOT UNDISPUTED
These mean a lot of different things to
different people.
Hopefully they’re of some use to you.
WIDELY RECOGNIZED,
BUT NOT UNDISPUTED
[:]?
Critical Concept in
Software Engineering?
Some think so, others disagree.
[:]?
Simple Enumeration of
Weaknesses in C++
See http://www.norvig.com/design-
patterns/
[:]?
Just Shared Vocabulary?
So, Let’s talk about Swift
(2.2 Version)
PATTERNS ARE
LANGUAGE SPECIFIC
[:]?
Critical Concept in
Software Engineering?
Some think so, others disagree.
[:]?
Simple Enumeration of
Weaknesses in C++
See http://www.norvig.com/design-
patterns/
[:]?
Just Shared Vocabulary?
So, Let’s talk about Swift
(2.2 Version)
& Cocoa
PATTERNS ARE LANGUAGE
(& FRAMEWORK) SPECIFIC
[:]?
Critical Concept in
Software Engineering?
Some think so, others disagree.
[:]?
Simple Enumeration of
Weaknesses in C++
See http://www.norvig.com/design-
patterns/
[:]?
Just Shared Vocabulary?
Design Patterns in Swift 2.2CocoaConf Austin 2016
PATTERN SOURCES FOR TODAY
8

Design Patterns in Swift 2.2CocoaConf Austin 2016
PATTERN SOURCES FOR TODAY
8

Classic Cocoa Design Patterns as
implemented in Swift
Design Patterns in Swift 2.2CocoaConf Austin 2016
PATTERN SOURCES FOR TODAY
8

Classic Cocoa Design Patterns as
implemented in Swift
Some Gang of Four Patterns that lend
themselves to Swift
Design Patterns in Swift 2.2CocoaConf Austin 2016
PATTERN SOURCES FOR TODAY
8

Classic Cocoa Design Patterns as
implemented in Swift
Some Gang of Four Patterns that lend
themselves to Swift
Patterns Apple Documents for Swift
Design Patterns in Swift 2.2CocoaConf Austin 2016
PATTERN SOURCES FOR TODAY
8

Classic Cocoa Design Patterns as
implemented in Swift
Some Gang of Four Patterns that lend
themselves to Swift
Patterns Apple Documents for Swift
Patterns from Other Languages
Design Patterns in Swift 2.2CocoaConf Austin 2016
PATTERN SOURCES FOR TODAY
8

Classic Cocoa Design Patterns as
implemented in Swift
Some Gang of Four Patterns that lend
themselves to Swift
Patterns Apple Documents for Swift
Patterns from Other Languages
Interesting/Potential Swift-specific Patterns

Design Patterns in Swift 2.2CocoaConf Austin 2016
DELEGATE PATTERN
9

Cocoa is said to favor “Composition over Inheritance.” Delegation
is one of the hallmarks of that strategy.
UITableViewDataSource and UITableViewDelegate are
examples.
CLASSIC COCOA PATTERN

Design Patterns in Swift 2.2CocoaConf Austin 2016
DELEGATE PATTERN
9

Cocoa is said to favor “Composition over Inheritance.” Delegation
is one of the hallmarks of that strategy.
UITableViewDataSource and UITableViewDelegate are
examples.
CLASSIC COCOA PATTERN
[:]?
Eases Construction
This is a big reason why we don’t have
to use the “Factory Pattern” in Cocoa/
Swift
Design Patterns in Swift 2.2CocoaConf Austin 2016
DELEGATE PATTERN IN SWIFT
10

var myDS : UITableViewDataSource?
let myTVC = UITableViewController()
let myTV = myTVC.tableView
let num = myDS?.tableView(myTV, numberOfRowsInSection: 0)
let firstHeader = myDS?.tableView?(myTV, titleForHeaderInSection: 0)
Design Patterns in Swift 2.2CocoaConf Austin 2016
DELEGATE PATTERN IN SWIFT
10

var myDS : UITableViewDataSource?
let myTVC = UITableViewController()
let myTV = myTVC.tableView
let num = myDS?.tableView(myTV, numberOfRowsInSection: 0)
let firstHeader = myDS?.tableView?(myTV, titleForHeaderInSection: 0)
1
Test Delegate Optional
The delegate itself must
not be nil (check with ?
and don’t (EVER) use !)
2
Test Method Exists
Put ? after method call to
test for @optional
implementation

Design Patterns in Swift 2.2CocoaConf Austin 2016
ERROR HANDLING PATTERN
11

In Cocoa, methods that produce errors take an NSError pointer
parameter last parameter.
In Swift 2, this is converted to a thrown ErrorType
NEW IN SWIFT 2 - HOOKS INTO COCOA’S (NSError**)

Design Patterns in Swift 2.2CocoaConf Austin 2016
ERROR HANDLING PATTERN
11

In Cocoa, methods that produce errors take an NSError pointer
parameter last parameter.
In Swift 2, this is converted to a thrown ErrorType
NEW IN SWIFT 2 - HOOKS INTO COCOA’S (NSError**)
[]
Not JAva’s Throw
try on each line that might
throw
catch for blocks
Design Patterns in Swift 2.2CocoaConf Austin 2016
ERROR HANDLING PATTERN IN SWIFT
12

- (BOOL)removeItemAtURL:(NSURL *)URL error:(NSError **)error {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *URL = [NSURL fileURLWithPath:@"/path/to/file"];
NSError *error = nil;
BOOL success = [fileManager removeItemAtURL:URL error:&error];
if (!success) {
NSLog(@"Error: %@", error.domain);
}
}
Design Patterns in Swift 2.2CocoaConf Austin 2016
ERROR HANDLING PATTERN IN SWIFT
12

func removeItemAtURL(URL: NSURL) throws {
let fileManager = NSFileManager.defaultManager()
let URL = NSURL.fileURLWithPath("/path/to/file")
do {
try fileManager.removeItemAtURL(URL)
} catch let error as NSError {
print("Error: (error.domain)")
}
}
- (BOOL)removeItemAtURL:(NSURL *)URL error:(NSError **)error {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *URL = [NSURL fileURLWithPath:@"/path/to/file"];
NSError *error = nil;
BOOL success = [fileManager removeItemAtURL:URL error:&error];
if (!success) {
NSLog(@"Error: %@", error.domain);
}
}

Design Patterns in Swift 2.2CocoaConf Austin 2016
OBSERVER PATTERN
13

Key-Value Observing lets you observe any well-behaved Cocoa
property (getter/setter).
It works in Swift *As Long As* the target isKindOf: NSObject
KVO IN SWIFT
Design Patterns in Swift 2.2CocoaConf Austin 2016
OBSERVER PATTERN IN SWIFT
14

Design Patterns in Swift 2.2CocoaConf Austin 2016
OBSERVER PATTERN IN SWIFT
14

objectToObserve.addObserver(self, forKeyPath: "myDate", options: .New,
context: &myContext)
Design Patterns in Swift 2.2CocoaConf Austin 2016
OBSERVER PATTERN IN SWIFT
14

objectToObserve.addObserver(self, forKeyPath: "myDate", options: .New,
context: &myContext)
override func observeValueForKeyPath(keyPath:String?, ofObject object: AnyObject?,
change: [ String : AnyObject ]?, context: UnsafeMutablePointer <Void>) {
if context == &myContext {
if let newValue = change?[NSKeyValueChangeNewKey] {
print("Date changed: (newValue)")
}
} else {
super.observeValueForKeyPath(keyPath, ofObject: object, change: change,
context: context)
}
}
Design Patterns in Swift 2.2CocoaConf Austin 2016
OBSERVER PATTERN IN SWIFT
14

objectToObserve.addObserver(self, forKeyPath: "myDate", options: .New,
context: &myContext)
override func observeValueForKeyPath(keyPath:String?, ofObject object: AnyObject?,
change: [ String : AnyObject ]?, context: UnsafeMutablePointer <Void>) {
if context == &myContext {
if let newValue = change?[NSKeyValueChangeNewKey] {
print("Date changed: (newValue)")
}
} else {
super.observeValueForKeyPath(keyPath, ofObject: object, change: change,
context: context)
}
}
objectToObserve.removeObserver(self, forKeyPath: "myDate", context:
&myContext)
Design Patterns in Swift 2.2CocoaConf Austin 2016
SWIFT-NATIVE KVO *MAYBE* IN SWIFT 4
15


Design Patterns in Swift 2.2CocoaConf Austin 2016
TARGET-ACTION PATTERN
16

In Objective-C, we use @selector to wrap the method name
when adding it as a target.
In Swift 1.x, we just used a String and still had no compiler
typo checking.
In Swift 2.2, we have #selector (SE-0022) and we FINALLY have
compiler type checking.
OSS FTW!!
ATTACH AN ACTION TO A UI ELEMENT
Design Patterns in Swift 2.2CocoaConf Austin 2016
TARGET-ACTION PATTERN IN SWIFT
17

Design Patterns in Swift 2.2CocoaConf Austin 2016
TARGET-ACTION PATTERN IN SWIFT
17

@objc func tappedButton(sender:UIButton!) {
print("tapped button")
}
Design Patterns in Swift 2.2CocoaConf Austin 2016
TARGET-ACTION PATTERN IN SWIFT
17

@objc func tappedButton(sender:UIButton!) {
print("tapped button")
}
#if swift(>=2.2)
myButton.addTarget(self, action: #selector(tappedButton),
forControlEvents: .TouchUpInside)
#else
myButton.addTarget(self, action: "tappedButton",
forControlEvents: .TouchUpInside)
#endif
Design Patterns in Swift 2.2CocoaConf Austin 2016
TARGET-ACTION PATTERN IN SWIFT
17

@objc func tappedButton(sender:UIButton!) {
print("tapped button")
}
#if swift(>=2.2)
myButton.addTarget(self, action: #selector(tappedButton),
forControlEvents: .TouchUpInside)
#else
myButton.addTarget(self, action: "tappedButton",
forControlEvents: .TouchUpInside)
#endif
1
Legacy Objective-C
Only @objc functions can
be made #selectors

Design Patterns in Swift 2.2CocoaConf Austin 2016
SINGLETON PATTERN
18

Fairly common in Cocoa, like [UIApplication
sharedApplication]
In Swift, they’re much simpler to declare (and like ObjC, never
dealloc’ed).
A static type property is guaranteed to be lazily initialized only
once, even when accessed across multiple threads
simultaneously.
SINGLETONS: GLOBAL SHARED INSTANCE
Design Patterns in Swift 2.2CocoaConf Austin 2016
SINGLETON PATTERN IN SWIFT
19

class Singleton {
static let sharedInstance = Singleton()
}
Design Patterns in Swift 2.2CocoaConf Austin 2016
SINGLETON PATTERN IN SWIFT
19

class Singleton {
static let sharedInstance = Singleton()
}
[]
Well, That Was Easy
Obj-C is so much more
complicated
[]
Not All Shared Instances Are Singletons
e.g. NSFileManager

Design Patterns in Swift 2.2CocoaConf Austin 2016
“INTROSPECTION PATTERN”
20

Apple’s Docs call this a pattern. It’s pretty much the same
functionality as we discussed for Delegation.
In my mind, “is this a kind of X class” and “does this implement Y
method” are very limited forms of Introspection.
DYNAMICALLY EXAMINE TYPES AT RUNTIME

Design Patterns in Swift 2.2CocoaConf Austin 2016
“INTROSPECTION PATTERN”
20

Apple’s Docs call this a pattern. It’s pretty much the same
functionality as we discussed for Delegation.
In my mind, “is this a kind of X class” and “does this implement Y
method” are very limited forms of Introspection.
DYNAMICALLY EXAMINE TYPES AT RUNTIME
[]
Not Very Useful
Way harder in pure Swift
than ObjC. Most attempts
bridge NSObjects.

Design Patterns in Swift 2.2CocoaConf Austin 2016
API AVAILABILITY CHECKING “PATTERN”
21

Swift does this at Compile time rather than Runtime (like ObjC).
Calling this a “Pattern” seems like a bit of a stretch to me, but
Apple does.
It is indisputably useful, though.
DECIDE WHETHER AN API IS AVAILABLE

Design Patterns in Swift 2.2CocoaConf Austin 2016
API AVAILABILITY CHECKING “PATTERN”
21

Swift does this at Compile time rather than Runtime (like ObjC).
Calling this a “Pattern” seems like a bit of a stretch to me, but
Apple does.
It is indisputably useful, though.
DECIDE WHETHER AN API IS AVAILABLE
[]
Also for Swift Version #s
Starting with Swift 2.2
Design Patterns in Swift 2.2CocoaConf Austin 2016
API AVAILABILITY CHECKING “PATTERN” IN SWIFT
22

if ([CLLocationManager
instancesRespondToSelector:@selector(requestWhenInUseAuthorization)]) {
// Method is available for use.
} else {
// Method is not available.
}
Design Patterns in Swift 2.2CocoaConf Austin 2016
API AVAILABILITY CHECKING “PATTERN” IN SWIFT
22

let locationManager = CLLocationManager()
if #available(iOS 8.0, OSX 10.10, *) {
locationManager.requestWhenInUseAuthorization()
}
let locationManager = CLLocationManager()
guard #available(iOS 8.0, OSX 10.10, *) else { return }
locationManager.requestWhenInUseAuthorization()
if ([CLLocationManager
instancesRespondToSelector:@selector(requestWhenInUseAuthorization)]) {
// Method is available for use.
} else {
// Method is not available.
}
Design Patterns in Swift 2.2CocoaConf Austin 2016
API AVAILABILITY CHECKING “PATTERN” IN SWIFT
22

let locationManager = CLLocationManager()
if #available(iOS 8.0, OSX 10.10, *) {
locationManager.requestWhenInUseAuthorization()
}
let locationManager = CLLocationManager()
guard #available(iOS 8.0, OSX 10.10, *) else { return }
locationManager.requestWhenInUseAuthorization()
if ([CLLocationManager
instancesRespondToSelector:@selector(requestWhenInUseAuthorization)]) {
// Method is available for use.
} else {
// Method is not available.
}
#if swift(>=2.2)
let tapped = #selector(tappedButton)
#endif

Design Patterns in Swift 2.2CocoaConf Austin 2016
VALUE TYPES/STRUCTS
23

Swift structs can have methods and implement protocols much
like classes, but they’re always passed by value (copied) rather
than passed by reference like objects (which makes them
generally thread-safe, amongst other things).
SWIFT HAS RICH VALUE-TYPES

Design Patterns in Swift 2.2CocoaConf Austin 2016
UBIQUITOUS IMMUTABILITY
24

Pretty much all Swift variables can be declared immutable (with
‘let’), not just certain types like ObjC.
Immutable variables are safer, thread-safe and side-effect free,
which is one claim to fame for functional languages.
NOT JUST FOR STRING/DICT/ARRAY ANYMORE

Design Patterns in Swift 2.2CocoaConf Austin 2016
SEPARATION OF ERRORS
25

Swift has several mechanisms to keep error checking outside and
separate from your business logic.
Checks often get moved from runtime to compile time, and
syntax allows for checks to be isolated to avoid further
complicating already-intricate code.
KEEP ERROR CHECKING AWAY FROM YOUR ALGORITHMS

Design Patterns in Swift 2.2CocoaConf Austin 2016
SEPARATION OF ERRORS 1: if let
26

Much safer than finding out the hard way at runtime.
Unless you use (!)
You don’t use (!), do you? (At least not in shipping apps…)
ENFORCES NIL-CHECK AT COMPILE TIME
Design Patterns in Swift 2.2CocoaConf Austin 2016
SEPARATION OF ERRORS IN SWIFT 1: if let
27

if let obj = obj {
//business logic about object goes here
}

Design Patterns in Swift 2.2CocoaConf Austin 2016
SEPARATION OF ERRORS 2: guard
28

Good for checking at the top of a method
Avoids the “if-let pyramid of doom”
LIKE if-let, BUT WITH CONTINUING SCOPE
Design Patterns in Swift 2.2CocoaConf Austin 2016
SEPARATION OF ERRORS IN SWIFT 2: guard
29

guard let obj = obj as? NSString
where obj.length > 0 else { return }
//business logic about object goes here

Design Patterns in Swift 2.2CocoaConf Austin 2016
SEPARATION OF ERRORS 3: defer
30

Make sure something happens as you exit a scope
SCHEDULE SOMETHING FOR LATER

Design Patterns in Swift 2.2CocoaConf Austin 2016
SEPARATION OF ERRORS 3: defer
30

Make sure something happens as you exit a scope
SCHEDULE SOMETHING FOR LATER
[]
Dearly Departed
@synchronized(dict) {
//use dict safely
}
Design Patterns in Swift 2.2CocoaConf Austin 2016
SEPARATION OF ERRORS IN SWIFT 3: defer
31

let lock = NSLock()
func useLock() {
lock.lock()
defer { lock.unlock() }
//use lock
}

Design Patterns in Swift 2.2CocoaConf Austin 2016
SEPARATION OF ERRORS: try?
32

The try syntax gets scattered all over your code.
Not exactly as good, but at least better than NSError** (or god
forbid C’s return codes and errno).
TRY IS A COUNTER-EXAMPLE THOUGH
Design Patterns in Swift 2.2CocoaConf Austin 2016
SEPARATION OF ERRORS IN SWIFT: try?
33

let url = try fileManager.URLForDirectory(.ApplicationDirectory,
inDomain: .UserDomainMask, appropriateForURL: nil, create: false)
try fileManager.removeItemAtURL(
url.URLByAppendingPathComponent("/path/to/file", isDirectory: true))
if let path = url.URLByAppendingPathComponent("/path/to/file").path {
let files = try fileManager.contentsOfDirectoryAtPath(path)
//do something with files
}
Design Patterns in Swift 2.2CocoaConf Austin 2016
SEPARATION OF ERRORS IN SWIFT: try?
33

let url = try fileManager.URLForDirectory(.ApplicationDirectory,
inDomain: .UserDomainMask, appropriateForURL: nil, create: false)
try fileManager.removeItemAtURL(
url.URLByAppendingPathComponent("/path/to/file", isDirectory: true))
if let path = url.URLByAppendingPathComponent("/path/to/file").path {
let files = try fileManager.contentsOfDirectoryAtPath(path)
//do something with files
}

Design Patterns in Swift 2.2CocoaConf Austin 2016
MONADS (A LA HASKELL MAYBE)
34

Very complicated, at least if you believe most people.
I remember being amazed - *Really amazed* at WWDC 2014
when I realized Apple stuck a Haskell Maybe Monad into Swift,
and used all of 1-character to do it.
FUNCTIONAL PROGRAMMING CONCEPT
Design Patterns in Swift 2.2CocoaConf Austin 2016
”MAYBE” MONAD
35

Design Patterns in Swift 2.2CocoaConf Austin 2016
”MAYBE” MONAD SWIFT OPTIONAL
36

Design Patterns in Swift 2.2CocoaConf Austin 2016
”MAYBE” MONAD SWIFT OPTIONAL
36

Optional

Design Patterns in Swift 2.2CocoaConf Austin 2016
”MAYBE” MONAD SWIFT OPTIONAL
36

Optional

! ?


Design Patterns in Swift 2.2CocoaConf Austin 2016
”MAYBE” MONAD SWIFT OPTIONAL
36

Optional

! ?


?
Design Patterns in Swift 2.2CocoaConf Austin 2016
”MAYBE” MONAD SWIFT OPTIONAL
36

Optional

! ?


?
Design Patterns in Swift 2.2CocoaConf Austin 2016
”MAYBE” MONAD SWIFT OPTIONAL
36

Optional

! ?


?
!
Design Patterns in Swift 2.2CocoaConf Austin 2016
”MAYBE” MONAD SWIFT OPTIONAL
36

Optional

! ?


?
!
[]
“!” Considered HARMFUL
I mean that in a “GOTO” sense.

Design Patterns in Swift 2.2CocoaConf Austin 2016
MAPREDUCE
37

Without this, the Internet as you know it would never have come
to exist, and least not when and how it did.
SEMINAL PAPER BEHIND EARLY GOOGLE
http://static.googleusercontent.com/media/research.google.com/es/us/archive/mapreduce-osdi04.pdf
Design Patterns in Swift 2.2CocoaConf Austin 2016
MAPREDUCE — MAP
38


Design Patterns in Swift 2.2CocoaConf Austin 2016
MAPREDUCE — MAP
38







 


Design Patterns in Swift 2.2CocoaConf Austin 2016
MAPREDUCE — MAP
38







 

[]
Rule of thumb
Same count,
Different Type
Design Patterns in Swift 2.2CocoaConf Austin 2016
MAPREDUCE — MAP IN SWIFT
39
let names = ["RedStripeOnWhite.jpg","OrangeStripeOnWhite.jpg",
"YellowStripeOnWhite.jpg","GreenStripeOnWhite.jpg",
"BlackHorizontal.jpg","BlueStripeOnWhite.jpg",
“IndigoStripeOnWhite.jpg”,"VioletStripeOnWhite.jpg"]
let images = names.map {
UIImage(named: $0)
}
Design Patterns in Swift 2.2CocoaConf Austin 2016
MAPREDUCE — MAP IN SWIFT
39
let names = ["RedStripeOnWhite.jpg","OrangeStripeOnWhite.jpg",
"YellowStripeOnWhite.jpg","GreenStripeOnWhite.jpg",
"BlackHorizontal.jpg","BlueStripeOnWhite.jpg",
“IndigoStripeOnWhite.jpg”,"VioletStripeOnWhite.jpg"]
let images = names.map {
UIImage(named: $0)
}
1
Real WorlD: JSON
I use this a lot to map
JSON Dict to Object/Struct,
but this is more visual
Design Patterns in Swift 2.2CocoaConf Austin 2016
MAPREDUCE — FILTER
40

Design Patterns in Swift 2.2CocoaConf Austin 2016
MAPREDUCE — FILTER
40









Design Patterns in Swift 2.2CocoaConf Austin 2016
MAPREDUCE — FILTER
40









[]
Rule of thumb
Same Type,
Lower Count
Design Patterns in Swift 2.2CocoaConf Austin 2016
MAPREDUCE — FILTER IN SWIFT
41

let colors = images.filter { !mono($0) }
Design Patterns in Swift 2.2CocoaConf Austin 2016
MAPREDUCE — FILTER IN SWIFT
41

let colors = images.filter { !mono($0) }
1
Real WorlD: JSON
I use this a lot to throw out
JSON Dicts I don’t need
Design Patterns in Swift 2.2CocoaConf Austin 2016
MAPREDUCE — REDUCE
42


Design Patterns in Swift 2.2CocoaConf Austin 2016
MAPREDUCE — REDUCE
42







Design Patterns in Swift 2.2CocoaConf Austin 2016
MAPREDUCE — REDUCE
42






[]
Rule of thumb
Different Type,
Count == 1
Design Patterns in Swift 2.2CocoaConf Austin 2016
MAPREDUCE — REDUCE
43

let rainbowImage = ciImages.reduce(CIImage()) { (s, img) -> CIImage? in
let f = CIFilter(name: "CISourceOverCompositing")
f?.setValue(s, forKey: kCIInputBackgroundImageKey)
f?.setValue(img, forKey:kCIInputImageKey)
return f?.outputImage
}
Design Patterns in Swift 2.2CocoaConf Austin 2016
MAPREDUCE — REDUCE
43

let rainbowImage = ciImages.reduce(CIImage()) { (s, img) -> CIImage? in
let f = CIFilter(name: "CISourceOverCompositing")
f?.setValue(s, forKey: kCIInputBackgroundImageKey)
f?.setValue(img, forKey:kCIInputImageKey)
return f?.outputImage
}
Design Patterns in Swift 2.2CocoaConf Austin 2016
MAPREDUCE — REDUCE
44

let rainbowImage = ciImages.reduce(CIImage()) { (s, img) -> CIImage? in
let f = CIFilter(name: "CISourceOverCompositing")
f?.setValue(s, forKey: kCIInputBackgroundImageKey)
f?.setValue(img, forKey:kCIInputImageKey)
return f?.outputImage
}
Design Patterns in Swift 2.2CocoaConf Austin 2016
MAPREDUCE — REDUCE
44

let rainbowImage = ciImages.reduce(CIImage()) { (s, img) -> CIImage? in
let f = CIFilter(name: "CISourceOverCompositing")
f?.setValue(s, forKey: kCIInputBackgroundImageKey)
f?.setValue(img, forKey:kCIInputImageKey)
return f?.outputImage
}
Design Patterns in Swift 2.2CocoaConf Austin 2016
MAPREDUCE — REDUCE
45

let rainbowImage = ciImages.reduce(CIImage()) { (s, img) -> CIImage? in
let f = CIFilter(name: "CISourceOverCompositing")
f?.setValue(s, forKey: kCIInputBackgroundImageKey)
f?.setValue(img, forKey:kCIInputImageKey)
return f?.outputImage
}
Design Patterns in Swift 2.2CocoaConf Austin 2016
MAPREDUCE — REDUCE
45

let rainbowImage = ciImages.reduce(CIImage()) { (s, img) -> CIImage? in
let f = CIFilter(name: "CISourceOverCompositing")
f?.setValue(s, forKey: kCIInputBackgroundImageKey)
f?.setValue(img, forKey:kCIInputImageKey)
return f?.outputImage
}
Design Patterns in Swift 2.2CocoaConf Austin 2016
MAPREDUCE — REDUCE
46

let rainbowImage = ciImages.reduce(CIImage()) { (s, img) -> CIImage? in
let f = CIFilter(name: "CISourceOverCompositing")
f?.setValue(s, forKey: kCIInputBackgroundImageKey)
f?.setValue(img, forKey:kCIInputImageKey)
return f?.outputImage
}
Design Patterns in Swift 2.2CocoaConf Austin 2016
MAPREDUCE — REDUCE
46

let rainbowImage = ciImages.reduce(CIImage()) { (s, img) -> CIImage? in
let f = CIFilter(name: "CISourceOverCompositing")
f?.setValue(s, forKey: kCIInputBackgroundImageKey)
f?.setValue(img, forKey:kCIInputImageKey)
return f?.outputImage
}
Design Patterns in Swift 2.2CocoaConf Austin 2016
MAPREDUCE — REDUCE
47

let rainbowImage = ciImages.reduce(CIImage()) { (s, img) -> CIImage? in
let f = CIFilter(name: "CISourceOverCompositing")
f?.setValue(s, forKey: kCIInputBackgroundImageKey)
f?.setValue(img, forKey:kCIInputImageKey)
return f?.outputImage
}
Design Patterns in Swift 2.2CocoaConf Austin 2016
MAPREDUCE — REDUCE
47

let rainbowImage = ciImages.reduce(CIImage()) { (s, img) -> CIImage? in
let f = CIFilter(name: "CISourceOverCompositing")
f?.setValue(s, forKey: kCIInputBackgroundImageKey)
f?.setValue(img, forKey:kCIInputImageKey)
return f?.outputImage
}
Design Patterns in Swift 2.2CocoaConf Austin 2016
MAPREDUCE — REDUCE
48

let rainbowImage = ciImages.reduce(CIImage()) { (s, img) -> CIImage? in
let f = CIFilter(name: "CISourceOverCompositing")
f?.setValue(s, forKey: kCIInputBackgroundImageKey)
f?.setValue(img, forKey:kCIInputImageKey)
return f?.outputImage
}
Design Patterns in Swift 2.2CocoaConf Austin 2016
MAPREDUCE — REDUCE
48

let rainbowImage = ciImages.reduce(CIImage()) { (s, img) -> CIImage? in
let f = CIFilter(name: "CISourceOverCompositing")
f?.setValue(s, forKey: kCIInputBackgroundImageKey)
f?.setValue(img, forKey:kCIInputImageKey)
return f?.outputImage
}
Design Patterns in Swift 2.2CocoaConf Austin 2016
MAPREDUCE — REDUCE
49

let rainbowImage = ciImages.reduce(CIImage()) { (s, img) -> CIImage? in
let f = CIFilter(name: "CISourceOverCompositing")
f?.setValue(s, forKey: kCIInputBackgroundImageKey)
f?.setValue(img, forKey:kCIInputImageKey)
return f?.outputImage
}
Design Patterns in Swift 2.2CocoaConf Austin 2016
MAPREDUCE — REDUCE
49

let rainbowImage = ciImages.reduce(CIImage()) { (s, img) -> CIImage? in
let f = CIFilter(name: "CISourceOverCompositing")
f?.setValue(s, forKey: kCIInputBackgroundImageKey)
f?.setValue(img, forKey:kCIInputImageKey)
return f?.outputImage
}

Design Patterns in Swift 2.2CocoaConf Austin 2016
MAPREDUCE - WAIT, WAIT, WAIT!!!
50

I have yet to see a case where map/reduce worked consistently
faster on iOS than the equivalent for..in loop, and often it can be
quite a bit slower.
BUT - learn it anyway.
ABOUT SWIFT’S MAPREDUCE PERFORMANCE…

Design Patterns in Swift 2.2CocoaConf Austin 2016
MAPREDUCE - SO WHAT’S THE POINT?
51

…or threads. Or at least, that was the design intent.
I still use it (although I don’t have to), because I’m used to it from
other languages (and I use it with dispatch_groups, despite the
fact they don’t fit on slides).
But Soon (SWIFT 4?? - see earlier email), we’ll have a Swift release
that focuses on asynchronous functionality. And THEN, it will
really, really matter.
MAPREDUCE SPLITS WORK BETWEEN MACHINES

Design Patterns in Swift 2.2CocoaConf Austin 2016
PROTOCOL DEFAULT IMPLEMENTATIONS
52

Protocol Implementations allow for much more useful protocols,
and for much more flexible implementations.
WHOLE NEW WORLD

Design Patterns in Swift 2.2CocoaConf Austin 2016
PROTOCOL EXTENSIONS (W/IMPLEMENTATIONS)
53

Protocol Implementations allow for much more useful protocols,
and for much more flexible implementations.
WHOLE NEW WORLD

Design Patterns in Swift 2.2CocoaConf Austin 2016
PROTOCOL EXTENSIONS (W/IMPLEMENTATIONS)
53

Protocol Implementations allow for much more useful protocols,
and for much more flexible implementations.
WHOLE NEW WORLD
1
Examples are Contrived
And they take a while to
set up, and I’m expecting
this is already going long
2
Big Topic
There’s a whole (VERY
GOOD) WWDC talk on just
this.
Speaking to you all is always a pleasure.
Thank You
Speaking to you all is always a pleasure.
Thank You

Mais conteúdo relacionado

Mais procurados

T3DD13 - Automated deployment for TYPO3 CMS (Workshop)
T3DD13 - Automated deployment for TYPO3 CMS (Workshop)T3DD13 - Automated deployment for TYPO3 CMS (Workshop)
T3DD13 - Automated deployment for TYPO3 CMS (Workshop)
Tobias Liebig
 
Openshift: Deployments for the rest of us
Openshift: Deployments for the rest of usOpenshift: Deployments for the rest of us
Openshift: Deployments for the rest of us
Anurag Patel
 
sbt 0.10 for beginners?
sbt 0.10 for beginners?sbt 0.10 for beginners?
sbt 0.10 for beginners?
k4200
 

Mais procurados (20)

Golang for PHP Developers: Dependency management with Glide
Golang for PHP Developers: Dependency management with GlideGolang for PHP Developers: Dependency management with Glide
Golang for PHP Developers: Dependency management with Glide
 
T3DD13 - Automated deployment for TYPO3 CMS (Workshop)
T3DD13 - Automated deployment for TYPO3 CMS (Workshop)T3DD13 - Automated deployment for TYPO3 CMS (Workshop)
T3DD13 - Automated deployment for TYPO3 CMS (Workshop)
 
Openshift: Deployments for the rest of us
Openshift: Deployments for the rest of usOpenshift: Deployments for the rest of us
Openshift: Deployments for the rest of us
 
sbt 0.10 for beginners?
sbt 0.10 for beginners?sbt 0.10 for beginners?
sbt 0.10 for beginners?
 
Lifthub (rpscala #31)
Lifthub (rpscala #31)Lifthub (rpscala #31)
Lifthub (rpscala #31)
 
Bgoug 2019.11 test your pl sql - not your patience
Bgoug 2019.11   test your pl sql - not your patienceBgoug 2019.11   test your pl sql - not your patience
Bgoug 2019.11 test your pl sql - not your patience
 
Golang workshop
Golang workshopGolang workshop
Golang workshop
 
Griffon: Swing just got fun again
Griffon: Swing just got fun againGriffon: Swing just got fun again
Griffon: Swing just got fun again
 
Let the contribution begin (EST futures)
Let the contribution begin  (EST futures)Let the contribution begin  (EST futures)
Let the contribution begin (EST futures)
 
Dead-Simple Async Control Flow with Coroutines
Dead-Simple Async Control Flow with CoroutinesDead-Simple Async Control Flow with Coroutines
Dead-Simple Async Control Flow with Coroutines
 
5 Simple Tips for Troubleshooting Your Kubernetes Pods
5 Simple Tips for Troubleshooting Your Kubernetes Pods5 Simple Tips for Troubleshooting Your Kubernetes Pods
5 Simple Tips for Troubleshooting Your Kubernetes Pods
 
Gitflow - Una metología para manejo de Branches
Gitflow - Una metología para manejo de BranchesGitflow - Una metología para manejo de Branches
Gitflow - Una metología para manejo de Branches
 
Node.js + influx + grafana
Node.js + influx + grafanaNode.js + influx + grafana
Node.js + influx + grafana
 
19 GitFlow #burningkeyboards
19 GitFlow #burningkeyboards19 GitFlow #burningkeyboards
19 GitFlow #burningkeyboards
 
Let the contribution begin
Let the contribution beginLet the contribution begin
Let the contribution begin
 
Frida Android run time hooking - Bhargav Gajera & Vitthal Shinde
Frida  Android run time hooking - Bhargav Gajera & Vitthal ShindeFrida  Android run time hooking - Bhargav Gajera & Vitthal Shinde
Frida Android run time hooking - Bhargav Gajera & Vitthal Shinde
 
Git hooks
Git hooksGit hooks
Git hooks
 
Killer R10K Workflow - PuppetConf 2014
Killer R10K Workflow - PuppetConf 2014Killer R10K Workflow - PuppetConf 2014
Killer R10K Workflow - PuppetConf 2014
 
Bamboo Hands on training 2016
Bamboo Hands on training 2016Bamboo Hands on training 2016
Bamboo Hands on training 2016
 
Enjoy privacy on Gitlab
Enjoy privacy on GitlabEnjoy privacy on Gitlab
Enjoy privacy on Gitlab
 

Semelhante a Swift 2.2 Design Patterns CocoaConf Austin 2016

Semelhante a Swift 2.2 Design Patterns CocoaConf Austin 2016 (20)

What's new in Swift 3
What's new in Swift 3What's new in Swift 3
What's new in Swift 3
 
The productive developer guide to Angular 2
The productive developer guide to Angular 2The productive developer guide to Angular 2
The productive developer guide to Angular 2
 
Front End Development for Back End Developers - UberConf 2017
Front End Development for Back End Developers - UberConf 2017Front End Development for Back End Developers - UberConf 2017
Front End Development for Back End Developers - UberConf 2017
 
Introduction to angular 2
Introduction to angular 2Introduction to angular 2
Introduction to angular 2
 
Angular2 & Native Script GDG DevFest 2016
Angular2 & Native Script GDG DevFest 2016Angular2 & Native Script GDG DevFest 2016
Angular2 & Native Script GDG DevFest 2016
 
Origins of Serverless
Origins of ServerlessOrigins of Serverless
Origins of Serverless
 
Building full-stack Node.js web apps with Visual Studio Code
Building full-stack Node.js web apps with Visual Studio CodeBuilding full-stack Node.js web apps with Visual Studio Code
Building full-stack Node.js web apps with Visual Studio Code
 
Creating Clean Code with AOP (WebExpo 2010)
Creating Clean Code with AOP (WebExpo 2010)Creating Clean Code with AOP (WebExpo 2010)
Creating Clean Code with AOP (WebExpo 2010)
 
You got database in my cloud!
You got database  in my cloud!You got database  in my cloud!
You got database in my cloud!
 
Front End Development for Back End Developers - Devoxx UK 2017
 Front End Development for Back End Developers - Devoxx UK 2017 Front End Development for Back End Developers - Devoxx UK 2017
Front End Development for Back End Developers - Devoxx UK 2017
 
Building Serverless applications with Python
Building Serverless applications with PythonBuilding Serverless applications with Python
Building Serverless applications with Python
 
Introduction to Angular 2
Introduction to Angular 2Introduction to Angular 2
Introduction to Angular 2
 
Follow your code: Node tracing
Follow your code: Node tracingFollow your code: Node tracing
Follow your code: Node tracing
 
Modern Release Engineering in a Nutshell - Why Researchers should Care!
Modern Release Engineering in a Nutshell - Why Researchers should Care!Modern Release Engineering in a Nutshell - Why Researchers should Care!
Modern Release Engineering in a Nutshell - Why Researchers should Care!
 
Spring Boot with Kotlin, Kofu and Coroutines
 Spring Boot with Kotlin, Kofu and Coroutines Spring Boot with Kotlin, Kofu and Coroutines
Spring Boot with Kotlin, Kofu and Coroutines
 
Dependent things dependency management for apple sw - slideshare
Dependent things   dependency management for apple sw - slideshareDependent things   dependency management for apple sw - slideshare
Dependent things dependency management for apple sw - slideshare
 
Front End Development for Backend Developers - GIDS 2019
Front End Development for Backend Developers - GIDS 2019Front End Development for Backend Developers - GIDS 2019
Front End Development for Backend Developers - GIDS 2019
 
Building Angular 2.0 applications with TypeScript
Building Angular 2.0 applications with TypeScriptBuilding Angular 2.0 applications with TypeScript
Building Angular 2.0 applications with TypeScript
 
esupdays21-upmcen-16020220284 portal7.pdf
esupdays21-upmcen-16020220284 portal7.pdfesupdays21-upmcen-16020220284 portal7.pdf
esupdays21-upmcen-16020220284 portal7.pdf
 
Basic overview of Angular
Basic overview of AngularBasic overview of Angular
Basic overview of Angular
 

Mais de Carl Brown

Cocoa coders 141113-watch
Cocoa coders 141113-watchCocoa coders 141113-watch
Cocoa coders 141113-watch
Carl Brown
 

Mais de Carl Brown (20)

GDPR, User Data, Privacy, and Your Apps
GDPR, User Data, Privacy, and Your AppsGDPR, User Data, Privacy, and Your Apps
GDPR, User Data, Privacy, and Your Apps
 
New in iOS 11.3b4 and Xcode 9.3b4
New in iOS 11.3b4 and Xcode 9.3b4New in iOS 11.3b4 and Xcode 9.3b4
New in iOS 11.3b4 and Xcode 9.3b4
 
Managing Memory in Swift (Yes, that's a thing)
Managing Memory in Swift (Yes, that's a thing)Managing Memory in Swift (Yes, that's a thing)
Managing Memory in Swift (Yes, that's a thing)
 
Better Swift from the Foundation up #tryswiftnyc17 09-06
Better Swift from the Foundation up #tryswiftnyc17 09-06Better Swift from the Foundation up #tryswiftnyc17 09-06
Better Swift from the Foundation up #tryswiftnyc17 09-06
 
Generics, the Swift ABI and you
Generics, the Swift ABI and youGenerics, the Swift ABI and you
Generics, the Swift ABI and you
 
Swift GUI Development without Xcode
Swift GUI Development without XcodeSwift GUI Development without Xcode
Swift GUI Development without Xcode
 
what's new in iOS10 2016-06-23
what's new in iOS10 2016-06-23what's new in iOS10 2016-06-23
what's new in iOS10 2016-06-23
 
Parse migration CocoaCoders April 28th, 2016
Parse migration CocoaCoders April 28th, 2016Parse migration CocoaCoders April 28th, 2016
Parse migration CocoaCoders April 28th, 2016
 
Advanced, Composable Collection Views, From CocoaCoders meetup Austin Feb 12,...
Advanced, Composable Collection Views, From CocoaCoders meetup Austin Feb 12,...Advanced, Composable Collection Views, From CocoaCoders meetup Austin Feb 12,...
Advanced, Composable Collection Views, From CocoaCoders meetup Austin Feb 12,...
 
Gcd cc-150205
Gcd cc-150205Gcd cc-150205
Gcd cc-150205
 
Cocoa coders 141113-watch
Cocoa coders 141113-watchCocoa coders 141113-watch
Cocoa coders 141113-watch
 
iOS8 and the new App Store
iOS8 and the new App Store   iOS8 and the new App Store
iOS8 and the new App Store
 
Dark Art of Software Estimation 360iDev2014
Dark Art of Software Estimation 360iDev2014Dark Art of Software Estimation 360iDev2014
Dark Art of Software Estimation 360iDev2014
 
Intro to cloud kit Cocoader.org 24 July 2014
Intro to cloud kit   Cocoader.org 24 July 2014Intro to cloud kit   Cocoader.org 24 July 2014
Intro to cloud kit Cocoader.org 24 July 2014
 
Welcome to Swift (CocoaCoder 6/12/14)
Welcome to Swift (CocoaCoder 6/12/14)Welcome to Swift (CocoaCoder 6/12/14)
Welcome to Swift (CocoaCoder 6/12/14)
 
Writing Apps that Can See: Getting Data from CoreImage to Computer Vision - ...
Writing Apps that Can See: Getting Data from CoreImage to Computer  Vision - ...Writing Apps that Can See: Getting Data from CoreImage to Computer  Vision - ...
Writing Apps that Can See: Getting Data from CoreImage to Computer Vision - ...
 
Introduction to Git Commands and Concepts
Introduction to Git Commands and ConceptsIntroduction to Git Commands and Concepts
Introduction to Git Commands and Concepts
 
REST/JSON/CoreData Example Code - A Tour
REST/JSON/CoreData Example Code - A TourREST/JSON/CoreData Example Code - A Tour
REST/JSON/CoreData Example Code - A Tour
 
360iDev iOS AntiPatterns
360iDev iOS AntiPatterns360iDev iOS AntiPatterns
360iDev iOS AntiPatterns
 
Chipmunk physics presentation
Chipmunk physics presentationChipmunk physics presentation
Chipmunk physics presentation
 

Último

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Último (20)

Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 

Swift 2.2 Design Patterns CocoaConf Austin 2016

  • 1. Swift 2.2 Design Patterns COCOACONF AUSTIN APRIL 2016 Skyline Credit: User:Argash [GFDL (http://www.gnu.org/copyleft/fdl.html) or CC BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0)], via Wikimedia Commons @CarlBrwn
  • 2. Swift 2.2 Design Patterns COCOACONF AUSTIN APRIL 2016 Skyline Credit: User:Argash [GFDL (http://www.gnu.org/copyleft/fdl.html) or CC BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0)], via Wikimedia Commons @CarlBrwn
  • 3. [:]? So, who is this idiot, anyway? INTRODUCTION @CarlBrwn
  • 4.
  • 5. These mean a lot of different things to different people. Hopefully they’re of some use to you. WIDELY RECOGNIZED, BUT NOT UNDISPUTED
  • 6. These mean a lot of different things to different people. Hopefully they’re of some use to you. WIDELY RECOGNIZED, BUT NOT UNDISPUTED [:]? Critical Concept in Software Engineering? Some think so, others disagree. [:]? Simple Enumeration of Weaknesses in C++ See http://www.norvig.com/design- patterns/ [:]? Just Shared Vocabulary?
  • 7. So, Let’s talk about Swift (2.2 Version) PATTERNS ARE LANGUAGE SPECIFIC [:]? Critical Concept in Software Engineering? Some think so, others disagree. [:]? Simple Enumeration of Weaknesses in C++ See http://www.norvig.com/design- patterns/ [:]? Just Shared Vocabulary?
  • 8. So, Let’s talk about Swift (2.2 Version) & Cocoa PATTERNS ARE LANGUAGE (& FRAMEWORK) SPECIFIC [:]? Critical Concept in Software Engineering? Some think so, others disagree. [:]? Simple Enumeration of Weaknesses in C++ See http://www.norvig.com/design- patterns/ [:]? Just Shared Vocabulary?
  • 9. Design Patterns in Swift 2.2CocoaConf Austin 2016 PATTERN SOURCES FOR TODAY 8 
  • 10. Design Patterns in Swift 2.2CocoaConf Austin 2016 PATTERN SOURCES FOR TODAY 8  Classic Cocoa Design Patterns as implemented in Swift
  • 11. Design Patterns in Swift 2.2CocoaConf Austin 2016 PATTERN SOURCES FOR TODAY 8  Classic Cocoa Design Patterns as implemented in Swift Some Gang of Four Patterns that lend themselves to Swift
  • 12. Design Patterns in Swift 2.2CocoaConf Austin 2016 PATTERN SOURCES FOR TODAY 8  Classic Cocoa Design Patterns as implemented in Swift Some Gang of Four Patterns that lend themselves to Swift Patterns Apple Documents for Swift
  • 13. Design Patterns in Swift 2.2CocoaConf Austin 2016 PATTERN SOURCES FOR TODAY 8  Classic Cocoa Design Patterns as implemented in Swift Some Gang of Four Patterns that lend themselves to Swift Patterns Apple Documents for Swift Patterns from Other Languages
  • 14. Design Patterns in Swift 2.2CocoaConf Austin 2016 PATTERN SOURCES FOR TODAY 8  Classic Cocoa Design Patterns as implemented in Swift Some Gang of Four Patterns that lend themselves to Swift Patterns Apple Documents for Swift Patterns from Other Languages Interesting/Potential Swift-specific Patterns
  • 15.  Design Patterns in Swift 2.2CocoaConf Austin 2016 DELEGATE PATTERN 9  Cocoa is said to favor “Composition over Inheritance.” Delegation is one of the hallmarks of that strategy. UITableViewDataSource and UITableViewDelegate are examples. CLASSIC COCOA PATTERN
  • 16.  Design Patterns in Swift 2.2CocoaConf Austin 2016 DELEGATE PATTERN 9  Cocoa is said to favor “Composition over Inheritance.” Delegation is one of the hallmarks of that strategy. UITableViewDataSource and UITableViewDelegate are examples. CLASSIC COCOA PATTERN [:]? Eases Construction This is a big reason why we don’t have to use the “Factory Pattern” in Cocoa/ Swift
  • 17. Design Patterns in Swift 2.2CocoaConf Austin 2016 DELEGATE PATTERN IN SWIFT 10  var myDS : UITableViewDataSource? let myTVC = UITableViewController() let myTV = myTVC.tableView let num = myDS?.tableView(myTV, numberOfRowsInSection: 0) let firstHeader = myDS?.tableView?(myTV, titleForHeaderInSection: 0)
  • 18. Design Patterns in Swift 2.2CocoaConf Austin 2016 DELEGATE PATTERN IN SWIFT 10  var myDS : UITableViewDataSource? let myTVC = UITableViewController() let myTV = myTVC.tableView let num = myDS?.tableView(myTV, numberOfRowsInSection: 0) let firstHeader = myDS?.tableView?(myTV, titleForHeaderInSection: 0) 1 Test Delegate Optional The delegate itself must not be nil (check with ? and don’t (EVER) use !) 2 Test Method Exists Put ? after method call to test for @optional implementation
  • 19.  Design Patterns in Swift 2.2CocoaConf Austin 2016 ERROR HANDLING PATTERN 11  In Cocoa, methods that produce errors take an NSError pointer parameter last parameter. In Swift 2, this is converted to a thrown ErrorType NEW IN SWIFT 2 - HOOKS INTO COCOA’S (NSError**)
  • 20.  Design Patterns in Swift 2.2CocoaConf Austin 2016 ERROR HANDLING PATTERN 11  In Cocoa, methods that produce errors take an NSError pointer parameter last parameter. In Swift 2, this is converted to a thrown ErrorType NEW IN SWIFT 2 - HOOKS INTO COCOA’S (NSError**) [] Not JAva’s Throw try on each line that might throw catch for blocks
  • 21. Design Patterns in Swift 2.2CocoaConf Austin 2016 ERROR HANDLING PATTERN IN SWIFT 12  - (BOOL)removeItemAtURL:(NSURL *)URL error:(NSError **)error { NSFileManager *fileManager = [NSFileManager defaultManager]; NSURL *URL = [NSURL fileURLWithPath:@"/path/to/file"]; NSError *error = nil; BOOL success = [fileManager removeItemAtURL:URL error:&error]; if (!success) { NSLog(@"Error: %@", error.domain); } }
  • 22. Design Patterns in Swift 2.2CocoaConf Austin 2016 ERROR HANDLING PATTERN IN SWIFT 12  func removeItemAtURL(URL: NSURL) throws { let fileManager = NSFileManager.defaultManager() let URL = NSURL.fileURLWithPath("/path/to/file") do { try fileManager.removeItemAtURL(URL) } catch let error as NSError { print("Error: (error.domain)") } } - (BOOL)removeItemAtURL:(NSURL *)URL error:(NSError **)error { NSFileManager *fileManager = [NSFileManager defaultManager]; NSURL *URL = [NSURL fileURLWithPath:@"/path/to/file"]; NSError *error = nil; BOOL success = [fileManager removeItemAtURL:URL error:&error]; if (!success) { NSLog(@"Error: %@", error.domain); } }
  • 23.  Design Patterns in Swift 2.2CocoaConf Austin 2016 OBSERVER PATTERN 13  Key-Value Observing lets you observe any well-behaved Cocoa property (getter/setter). It works in Swift *As Long As* the target isKindOf: NSObject KVO IN SWIFT
  • 24. Design Patterns in Swift 2.2CocoaConf Austin 2016 OBSERVER PATTERN IN SWIFT 14 
  • 25. Design Patterns in Swift 2.2CocoaConf Austin 2016 OBSERVER PATTERN IN SWIFT 14  objectToObserve.addObserver(self, forKeyPath: "myDate", options: .New, context: &myContext)
  • 26. Design Patterns in Swift 2.2CocoaConf Austin 2016 OBSERVER PATTERN IN SWIFT 14  objectToObserve.addObserver(self, forKeyPath: "myDate", options: .New, context: &myContext) override func observeValueForKeyPath(keyPath:String?, ofObject object: AnyObject?, change: [ String : AnyObject ]?, context: UnsafeMutablePointer <Void>) { if context == &myContext { if let newValue = change?[NSKeyValueChangeNewKey] { print("Date changed: (newValue)") } } else { super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context) } }
  • 27. Design Patterns in Swift 2.2CocoaConf Austin 2016 OBSERVER PATTERN IN SWIFT 14  objectToObserve.addObserver(self, forKeyPath: "myDate", options: .New, context: &myContext) override func observeValueForKeyPath(keyPath:String?, ofObject object: AnyObject?, change: [ String : AnyObject ]?, context: UnsafeMutablePointer <Void>) { if context == &myContext { if let newValue = change?[NSKeyValueChangeNewKey] { print("Date changed: (newValue)") } } else { super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context) } } objectToObserve.removeObserver(self, forKeyPath: "myDate", context: &myContext)
  • 28. Design Patterns in Swift 2.2CocoaConf Austin 2016 SWIFT-NATIVE KVO *MAYBE* IN SWIFT 4 15 
  • 29.  Design Patterns in Swift 2.2CocoaConf Austin 2016 TARGET-ACTION PATTERN 16  In Objective-C, we use @selector to wrap the method name when adding it as a target. In Swift 1.x, we just used a String and still had no compiler typo checking. In Swift 2.2, we have #selector (SE-0022) and we FINALLY have compiler type checking. OSS FTW!! ATTACH AN ACTION TO A UI ELEMENT
  • 30. Design Patterns in Swift 2.2CocoaConf Austin 2016 TARGET-ACTION PATTERN IN SWIFT 17 
  • 31. Design Patterns in Swift 2.2CocoaConf Austin 2016 TARGET-ACTION PATTERN IN SWIFT 17  @objc func tappedButton(sender:UIButton!) { print("tapped button") }
  • 32. Design Patterns in Swift 2.2CocoaConf Austin 2016 TARGET-ACTION PATTERN IN SWIFT 17  @objc func tappedButton(sender:UIButton!) { print("tapped button") } #if swift(>=2.2) myButton.addTarget(self, action: #selector(tappedButton), forControlEvents: .TouchUpInside) #else myButton.addTarget(self, action: "tappedButton", forControlEvents: .TouchUpInside) #endif
  • 33. Design Patterns in Swift 2.2CocoaConf Austin 2016 TARGET-ACTION PATTERN IN SWIFT 17  @objc func tappedButton(sender:UIButton!) { print("tapped button") } #if swift(>=2.2) myButton.addTarget(self, action: #selector(tappedButton), forControlEvents: .TouchUpInside) #else myButton.addTarget(self, action: "tappedButton", forControlEvents: .TouchUpInside) #endif 1 Legacy Objective-C Only @objc functions can be made #selectors
  • 34.  Design Patterns in Swift 2.2CocoaConf Austin 2016 SINGLETON PATTERN 18  Fairly common in Cocoa, like [UIApplication sharedApplication] In Swift, they’re much simpler to declare (and like ObjC, never dealloc’ed). A static type property is guaranteed to be lazily initialized only once, even when accessed across multiple threads simultaneously. SINGLETONS: GLOBAL SHARED INSTANCE
  • 35. Design Patterns in Swift 2.2CocoaConf Austin 2016 SINGLETON PATTERN IN SWIFT 19  class Singleton { static let sharedInstance = Singleton() }
  • 36. Design Patterns in Swift 2.2CocoaConf Austin 2016 SINGLETON PATTERN IN SWIFT 19  class Singleton { static let sharedInstance = Singleton() } [] Well, That Was Easy Obj-C is so much more complicated [] Not All Shared Instances Are Singletons e.g. NSFileManager
  • 37.  Design Patterns in Swift 2.2CocoaConf Austin 2016 “INTROSPECTION PATTERN” 20  Apple’s Docs call this a pattern. It’s pretty much the same functionality as we discussed for Delegation. In my mind, “is this a kind of X class” and “does this implement Y method” are very limited forms of Introspection. DYNAMICALLY EXAMINE TYPES AT RUNTIME
  • 38.  Design Patterns in Swift 2.2CocoaConf Austin 2016 “INTROSPECTION PATTERN” 20  Apple’s Docs call this a pattern. It’s pretty much the same functionality as we discussed for Delegation. In my mind, “is this a kind of X class” and “does this implement Y method” are very limited forms of Introspection. DYNAMICALLY EXAMINE TYPES AT RUNTIME [] Not Very Useful Way harder in pure Swift than ObjC. Most attempts bridge NSObjects.
  • 39.  Design Patterns in Swift 2.2CocoaConf Austin 2016 API AVAILABILITY CHECKING “PATTERN” 21  Swift does this at Compile time rather than Runtime (like ObjC). Calling this a “Pattern” seems like a bit of a stretch to me, but Apple does. It is indisputably useful, though. DECIDE WHETHER AN API IS AVAILABLE
  • 40.  Design Patterns in Swift 2.2CocoaConf Austin 2016 API AVAILABILITY CHECKING “PATTERN” 21  Swift does this at Compile time rather than Runtime (like ObjC). Calling this a “Pattern” seems like a bit of a stretch to me, but Apple does. It is indisputably useful, though. DECIDE WHETHER AN API IS AVAILABLE [] Also for Swift Version #s Starting with Swift 2.2
  • 41. Design Patterns in Swift 2.2CocoaConf Austin 2016 API AVAILABILITY CHECKING “PATTERN” IN SWIFT 22  if ([CLLocationManager instancesRespondToSelector:@selector(requestWhenInUseAuthorization)]) { // Method is available for use. } else { // Method is not available. }
  • 42. Design Patterns in Swift 2.2CocoaConf Austin 2016 API AVAILABILITY CHECKING “PATTERN” IN SWIFT 22  let locationManager = CLLocationManager() if #available(iOS 8.0, OSX 10.10, *) { locationManager.requestWhenInUseAuthorization() } let locationManager = CLLocationManager() guard #available(iOS 8.0, OSX 10.10, *) else { return } locationManager.requestWhenInUseAuthorization() if ([CLLocationManager instancesRespondToSelector:@selector(requestWhenInUseAuthorization)]) { // Method is available for use. } else { // Method is not available. }
  • 43. Design Patterns in Swift 2.2CocoaConf Austin 2016 API AVAILABILITY CHECKING “PATTERN” IN SWIFT 22  let locationManager = CLLocationManager() if #available(iOS 8.0, OSX 10.10, *) { locationManager.requestWhenInUseAuthorization() } let locationManager = CLLocationManager() guard #available(iOS 8.0, OSX 10.10, *) else { return } locationManager.requestWhenInUseAuthorization() if ([CLLocationManager instancesRespondToSelector:@selector(requestWhenInUseAuthorization)]) { // Method is available for use. } else { // Method is not available. } #if swift(>=2.2) let tapped = #selector(tappedButton) #endif
  • 44.  Design Patterns in Swift 2.2CocoaConf Austin 2016 VALUE TYPES/STRUCTS 23  Swift structs can have methods and implement protocols much like classes, but they’re always passed by value (copied) rather than passed by reference like objects (which makes them generally thread-safe, amongst other things). SWIFT HAS RICH VALUE-TYPES
  • 45.  Design Patterns in Swift 2.2CocoaConf Austin 2016 UBIQUITOUS IMMUTABILITY 24  Pretty much all Swift variables can be declared immutable (with ‘let’), not just certain types like ObjC. Immutable variables are safer, thread-safe and side-effect free, which is one claim to fame for functional languages. NOT JUST FOR STRING/DICT/ARRAY ANYMORE
  • 46.  Design Patterns in Swift 2.2CocoaConf Austin 2016 SEPARATION OF ERRORS 25  Swift has several mechanisms to keep error checking outside and separate from your business logic. Checks often get moved from runtime to compile time, and syntax allows for checks to be isolated to avoid further complicating already-intricate code. KEEP ERROR CHECKING AWAY FROM YOUR ALGORITHMS
  • 47.  Design Patterns in Swift 2.2CocoaConf Austin 2016 SEPARATION OF ERRORS 1: if let 26  Much safer than finding out the hard way at runtime. Unless you use (!) You don’t use (!), do you? (At least not in shipping apps…) ENFORCES NIL-CHECK AT COMPILE TIME
  • 48. Design Patterns in Swift 2.2CocoaConf Austin 2016 SEPARATION OF ERRORS IN SWIFT 1: if let 27  if let obj = obj { //business logic about object goes here }
  • 49.  Design Patterns in Swift 2.2CocoaConf Austin 2016 SEPARATION OF ERRORS 2: guard 28  Good for checking at the top of a method Avoids the “if-let pyramid of doom” LIKE if-let, BUT WITH CONTINUING SCOPE
  • 50. Design Patterns in Swift 2.2CocoaConf Austin 2016 SEPARATION OF ERRORS IN SWIFT 2: guard 29  guard let obj = obj as? NSString where obj.length > 0 else { return } //business logic about object goes here
  • 51.  Design Patterns in Swift 2.2CocoaConf Austin 2016 SEPARATION OF ERRORS 3: defer 30  Make sure something happens as you exit a scope SCHEDULE SOMETHING FOR LATER
  • 52.  Design Patterns in Swift 2.2CocoaConf Austin 2016 SEPARATION OF ERRORS 3: defer 30  Make sure something happens as you exit a scope SCHEDULE SOMETHING FOR LATER [] Dearly Departed @synchronized(dict) { //use dict safely }
  • 53. Design Patterns in Swift 2.2CocoaConf Austin 2016 SEPARATION OF ERRORS IN SWIFT 3: defer 31  let lock = NSLock() func useLock() { lock.lock() defer { lock.unlock() } //use lock }
  • 54.  Design Patterns in Swift 2.2CocoaConf Austin 2016 SEPARATION OF ERRORS: try? 32  The try syntax gets scattered all over your code. Not exactly as good, but at least better than NSError** (or god forbid C’s return codes and errno). TRY IS A COUNTER-EXAMPLE THOUGH
  • 55. Design Patterns in Swift 2.2CocoaConf Austin 2016 SEPARATION OF ERRORS IN SWIFT: try? 33  let url = try fileManager.URLForDirectory(.ApplicationDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: false) try fileManager.removeItemAtURL( url.URLByAppendingPathComponent("/path/to/file", isDirectory: true)) if let path = url.URLByAppendingPathComponent("/path/to/file").path { let files = try fileManager.contentsOfDirectoryAtPath(path) //do something with files }
  • 56. Design Patterns in Swift 2.2CocoaConf Austin 2016 SEPARATION OF ERRORS IN SWIFT: try? 33  let url = try fileManager.URLForDirectory(.ApplicationDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: false) try fileManager.removeItemAtURL( url.URLByAppendingPathComponent("/path/to/file", isDirectory: true)) if let path = url.URLByAppendingPathComponent("/path/to/file").path { let files = try fileManager.contentsOfDirectoryAtPath(path) //do something with files }
  • 57.  Design Patterns in Swift 2.2CocoaConf Austin 2016 MONADS (A LA HASKELL MAYBE) 34  Very complicated, at least if you believe most people. I remember being amazed - *Really amazed* at WWDC 2014 when I realized Apple stuck a Haskell Maybe Monad into Swift, and used all of 1-character to do it. FUNCTIONAL PROGRAMMING CONCEPT
  • 58. Design Patterns in Swift 2.2CocoaConf Austin 2016 ”MAYBE” MONAD 35 
  • 59. Design Patterns in Swift 2.2CocoaConf Austin 2016 ”MAYBE” MONAD SWIFT OPTIONAL 36 
  • 60. Design Patterns in Swift 2.2CocoaConf Austin 2016 ”MAYBE” MONAD SWIFT OPTIONAL 36  Optional 
  • 61. Design Patterns in Swift 2.2CocoaConf Austin 2016 ”MAYBE” MONAD SWIFT OPTIONAL 36  Optional  ! ?  
  • 62. Design Patterns in Swift 2.2CocoaConf Austin 2016 ”MAYBE” MONAD SWIFT OPTIONAL 36  Optional  ! ?   ?
  • 63. Design Patterns in Swift 2.2CocoaConf Austin 2016 ”MAYBE” MONAD SWIFT OPTIONAL 36  Optional  ! ?   ?
  • 64. Design Patterns in Swift 2.2CocoaConf Austin 2016 ”MAYBE” MONAD SWIFT OPTIONAL 36  Optional  ! ?   ? !
  • 65. Design Patterns in Swift 2.2CocoaConf Austin 2016 ”MAYBE” MONAD SWIFT OPTIONAL 36  Optional  ! ?   ? ! [] “!” Considered HARMFUL I mean that in a “GOTO” sense.
  • 66.  Design Patterns in Swift 2.2CocoaConf Austin 2016 MAPREDUCE 37  Without this, the Internet as you know it would never have come to exist, and least not when and how it did. SEMINAL PAPER BEHIND EARLY GOOGLE http://static.googleusercontent.com/media/research.google.com/es/us/archive/mapreduce-osdi04.pdf
  • 67. Design Patterns in Swift 2.2CocoaConf Austin 2016 MAPREDUCE — MAP 38 
  • 68.  Design Patterns in Swift 2.2CocoaConf Austin 2016 MAPREDUCE — MAP 38          
  • 69.  Design Patterns in Swift 2.2CocoaConf Austin 2016 MAPREDUCE — MAP 38           [] Rule of thumb Same count, Different Type
  • 70. Design Patterns in Swift 2.2CocoaConf Austin 2016 MAPREDUCE — MAP IN SWIFT 39 let names = ["RedStripeOnWhite.jpg","OrangeStripeOnWhite.jpg", "YellowStripeOnWhite.jpg","GreenStripeOnWhite.jpg", "BlackHorizontal.jpg","BlueStripeOnWhite.jpg", “IndigoStripeOnWhite.jpg”,"VioletStripeOnWhite.jpg"] let images = names.map { UIImage(named: $0) }
  • 71. Design Patterns in Swift 2.2CocoaConf Austin 2016 MAPREDUCE — MAP IN SWIFT 39 let names = ["RedStripeOnWhite.jpg","OrangeStripeOnWhite.jpg", "YellowStripeOnWhite.jpg","GreenStripeOnWhite.jpg", "BlackHorizontal.jpg","BlueStripeOnWhite.jpg", “IndigoStripeOnWhite.jpg”,"VioletStripeOnWhite.jpg"] let images = names.map { UIImage(named: $0) } 1 Real WorlD: JSON I use this a lot to map JSON Dict to Object/Struct, but this is more visual
  • 72. Design Patterns in Swift 2.2CocoaConf Austin 2016 MAPREDUCE — FILTER 40 
  • 73. Design Patterns in Swift 2.2CocoaConf Austin 2016 MAPREDUCE — FILTER 40         
  • 74. Design Patterns in Swift 2.2CocoaConf Austin 2016 MAPREDUCE — FILTER 40          [] Rule of thumb Same Type, Lower Count
  • 75. Design Patterns in Swift 2.2CocoaConf Austin 2016 MAPREDUCE — FILTER IN SWIFT 41  let colors = images.filter { !mono($0) }
  • 76. Design Patterns in Swift 2.2CocoaConf Austin 2016 MAPREDUCE — FILTER IN SWIFT 41  let colors = images.filter { !mono($0) } 1 Real WorlD: JSON I use this a lot to throw out JSON Dicts I don’t need
  • 77. Design Patterns in Swift 2.2CocoaConf Austin 2016 MAPREDUCE — REDUCE 42 
  • 78.  Design Patterns in Swift 2.2CocoaConf Austin 2016 MAPREDUCE — REDUCE 42      
  • 79.  Design Patterns in Swift 2.2CocoaConf Austin 2016 MAPREDUCE — REDUCE 42       [] Rule of thumb Different Type, Count == 1
  • 80. Design Patterns in Swift 2.2CocoaConf Austin 2016 MAPREDUCE — REDUCE 43  let rainbowImage = ciImages.reduce(CIImage()) { (s, img) -> CIImage? in let f = CIFilter(name: "CISourceOverCompositing") f?.setValue(s, forKey: kCIInputBackgroundImageKey) f?.setValue(img, forKey:kCIInputImageKey) return f?.outputImage }
  • 81. Design Patterns in Swift 2.2CocoaConf Austin 2016 MAPREDUCE — REDUCE 43  let rainbowImage = ciImages.reduce(CIImage()) { (s, img) -> CIImage? in let f = CIFilter(name: "CISourceOverCompositing") f?.setValue(s, forKey: kCIInputBackgroundImageKey) f?.setValue(img, forKey:kCIInputImageKey) return f?.outputImage }
  • 82. Design Patterns in Swift 2.2CocoaConf Austin 2016 MAPREDUCE — REDUCE 44  let rainbowImage = ciImages.reduce(CIImage()) { (s, img) -> CIImage? in let f = CIFilter(name: "CISourceOverCompositing") f?.setValue(s, forKey: kCIInputBackgroundImageKey) f?.setValue(img, forKey:kCIInputImageKey) return f?.outputImage }
  • 83. Design Patterns in Swift 2.2CocoaConf Austin 2016 MAPREDUCE — REDUCE 44  let rainbowImage = ciImages.reduce(CIImage()) { (s, img) -> CIImage? in let f = CIFilter(name: "CISourceOverCompositing") f?.setValue(s, forKey: kCIInputBackgroundImageKey) f?.setValue(img, forKey:kCIInputImageKey) return f?.outputImage }
  • 84. Design Patterns in Swift 2.2CocoaConf Austin 2016 MAPREDUCE — REDUCE 45  let rainbowImage = ciImages.reduce(CIImage()) { (s, img) -> CIImage? in let f = CIFilter(name: "CISourceOverCompositing") f?.setValue(s, forKey: kCIInputBackgroundImageKey) f?.setValue(img, forKey:kCIInputImageKey) return f?.outputImage }
  • 85. Design Patterns in Swift 2.2CocoaConf Austin 2016 MAPREDUCE — REDUCE 45  let rainbowImage = ciImages.reduce(CIImage()) { (s, img) -> CIImage? in let f = CIFilter(name: "CISourceOverCompositing") f?.setValue(s, forKey: kCIInputBackgroundImageKey) f?.setValue(img, forKey:kCIInputImageKey) return f?.outputImage }
  • 86. Design Patterns in Swift 2.2CocoaConf Austin 2016 MAPREDUCE — REDUCE 46  let rainbowImage = ciImages.reduce(CIImage()) { (s, img) -> CIImage? in let f = CIFilter(name: "CISourceOverCompositing") f?.setValue(s, forKey: kCIInputBackgroundImageKey) f?.setValue(img, forKey:kCIInputImageKey) return f?.outputImage }
  • 87. Design Patterns in Swift 2.2CocoaConf Austin 2016 MAPREDUCE — REDUCE 46  let rainbowImage = ciImages.reduce(CIImage()) { (s, img) -> CIImage? in let f = CIFilter(name: "CISourceOverCompositing") f?.setValue(s, forKey: kCIInputBackgroundImageKey) f?.setValue(img, forKey:kCIInputImageKey) return f?.outputImage }
  • 88. Design Patterns in Swift 2.2CocoaConf Austin 2016 MAPREDUCE — REDUCE 47  let rainbowImage = ciImages.reduce(CIImage()) { (s, img) -> CIImage? in let f = CIFilter(name: "CISourceOverCompositing") f?.setValue(s, forKey: kCIInputBackgroundImageKey) f?.setValue(img, forKey:kCIInputImageKey) return f?.outputImage }
  • 89. Design Patterns in Swift 2.2CocoaConf Austin 2016 MAPREDUCE — REDUCE 47  let rainbowImage = ciImages.reduce(CIImage()) { (s, img) -> CIImage? in let f = CIFilter(name: "CISourceOverCompositing") f?.setValue(s, forKey: kCIInputBackgroundImageKey) f?.setValue(img, forKey:kCIInputImageKey) return f?.outputImage }
  • 90. Design Patterns in Swift 2.2CocoaConf Austin 2016 MAPREDUCE — REDUCE 48  let rainbowImage = ciImages.reduce(CIImage()) { (s, img) -> CIImage? in let f = CIFilter(name: "CISourceOverCompositing") f?.setValue(s, forKey: kCIInputBackgroundImageKey) f?.setValue(img, forKey:kCIInputImageKey) return f?.outputImage }
  • 91. Design Patterns in Swift 2.2CocoaConf Austin 2016 MAPREDUCE — REDUCE 48  let rainbowImage = ciImages.reduce(CIImage()) { (s, img) -> CIImage? in let f = CIFilter(name: "CISourceOverCompositing") f?.setValue(s, forKey: kCIInputBackgroundImageKey) f?.setValue(img, forKey:kCIInputImageKey) return f?.outputImage }
  • 92. Design Patterns in Swift 2.2CocoaConf Austin 2016 MAPREDUCE — REDUCE 49  let rainbowImage = ciImages.reduce(CIImage()) { (s, img) -> CIImage? in let f = CIFilter(name: "CISourceOverCompositing") f?.setValue(s, forKey: kCIInputBackgroundImageKey) f?.setValue(img, forKey:kCIInputImageKey) return f?.outputImage }
  • 93. Design Patterns in Swift 2.2CocoaConf Austin 2016 MAPREDUCE — REDUCE 49  let rainbowImage = ciImages.reduce(CIImage()) { (s, img) -> CIImage? in let f = CIFilter(name: "CISourceOverCompositing") f?.setValue(s, forKey: kCIInputBackgroundImageKey) f?.setValue(img, forKey:kCIInputImageKey) return f?.outputImage }
  • 94.  Design Patterns in Swift 2.2CocoaConf Austin 2016 MAPREDUCE - WAIT, WAIT, WAIT!!! 50  I have yet to see a case where map/reduce worked consistently faster on iOS than the equivalent for..in loop, and often it can be quite a bit slower. BUT - learn it anyway. ABOUT SWIFT’S MAPREDUCE PERFORMANCE…
  • 95.  Design Patterns in Swift 2.2CocoaConf Austin 2016 MAPREDUCE - SO WHAT’S THE POINT? 51  …or threads. Or at least, that was the design intent. I still use it (although I don’t have to), because I’m used to it from other languages (and I use it with dispatch_groups, despite the fact they don’t fit on slides). But Soon (SWIFT 4?? - see earlier email), we’ll have a Swift release that focuses on asynchronous functionality. And THEN, it will really, really matter. MAPREDUCE SPLITS WORK BETWEEN MACHINES
  • 96.  Design Patterns in Swift 2.2CocoaConf Austin 2016 PROTOCOL DEFAULT IMPLEMENTATIONS 52  Protocol Implementations allow for much more useful protocols, and for much more flexible implementations. WHOLE NEW WORLD
  • 97.  Design Patterns in Swift 2.2CocoaConf Austin 2016 PROTOCOL EXTENSIONS (W/IMPLEMENTATIONS) 53  Protocol Implementations allow for much more useful protocols, and for much more flexible implementations. WHOLE NEW WORLD
  • 98.  Design Patterns in Swift 2.2CocoaConf Austin 2016 PROTOCOL EXTENSIONS (W/IMPLEMENTATIONS) 53  Protocol Implementations allow for much more useful protocols, and for much more flexible implementations. WHOLE NEW WORLD 1 Examples are Contrived And they take a while to set up, and I’m expecting this is already going long 2 Big Topic There’s a whole (VERY GOOD) WWDC talk on just this.
  • 99. Speaking to you all is always a pleasure. Thank You
  • 100. Speaking to you all is always a pleasure. Thank You