Slide 1

Slide 1 text

Building Functional Apps in Swift for iOS & OS X

Slide 2

Slide 2 text

I’m @soffes

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

iOS & OS X

Slide 13

Slide 13 text

Sharing is Caring™

Slide 14

Slide 14 text

Build for the challenge

Slide 15

Slide 15 text

usewhiskey.com

Slide 16

Slide 16 text

So, sharing…

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

Share what makes sense.

Slide 21

Slide 21 text

How can we share as much as possible?

Slide 22

Slide 22 text

Touch UIKit & AppKit as little as possible.

Slide 23

Slide 23 text

Clever Things Functional Things

Slide 24

Slide 24 text

Functional

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

Models are structs.

Slide 28

Slide 28 text

struct Redaction: Hashable, Equatable { let UUID: String let type: RedactionType var rect: CGRect // ... }

Slide 29

Slide 29 text

enum RedactionType: Int, Printable { case Pixelate, Blur, BlackBar var description: String { switch self { case .Pixelate: return string("PIXELATE") case .Blur: return string("BLUR") case .BlackBar: return string("BLACK_BAR") } } static var all: [RedactionType] { return [.Pixelate, .Blur, .BlackBar] } }

Slide 30

Slide 30 text

private func bundle() -> NSBundle? { return NSBundle(forClass: RedactedView.self) } func string(key: String) -> String { if let bundle = bundle() { return bundle.localizedStringForKey(key, value: nil, table: nil) } return key }

Slide 31

Slide 31 text

Core Image

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

func filter(image: CIImage, preprocessor: Preprocessor = Redaction.preprocess) -> CIFilter { let extent = image.extent() let scaledRect = rectForBounds(extent).flippedInRect(extent) let processed = preprocessor(image: image, type: type) return CIFilter(name: "CISourceOverCompositing", withInputParameters: [ "inputImage": processed.imageByCroppingToRect(scaledRect) ]) }

Slide 34

Slide 34 text

typealias Preprocessor = (image: CIImage, type: RedactionType) -> CIImage

Slide 35

Slide 35 text

Function types, neat.

Slide 36

Slide 36 text

static func preprocess(image: CIImage, type: RedactionType) -> CIImage { switch type { case .Pixelate: return // ... case .Blur: return // ... case .BlackBar: return // ... } }

Slide 37

Slide 37 text

Is blur secure?

Slide 38

Slide 38 text

5ebe2294ecd0e0f08eab7690d2a6ee69 “a” 0cc175b9c0f1b6a831c399e269772661 “b” 92eb5ffee6ae2fec3ad71c777531578f “c” 4a8a08f09d37b73795649038408b5f33 “d” 8277e0910d750195b448797616e091ad “e” e1671797c52e15f763380b45e841ec32

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

281,219,526 six letter words

Slide 44

Slide 44 text

281,219,526 × Type Faces × Font Sizes

Slide 45

Slide 45 text

Obfuscate, then blur

Slide 46

Slide 46 text

func filter(image: CIImage, preprocessor: Preprocessor = Redaction.preprocess) -> CIFilter { let extent = image.extent() let scaledRect = rectForBounds(extent).flippedInRect(extent) let processed = preprocessor(image: image, type: type) return CIFilter(name: "CISourceOverCompositing", withInputParameters: [ "inputImage": processed.imageByCroppingToRect(scaledRect) ]) }

Slide 47

Slide 47 text

let filter = redaction.filter(image)

Slide 48

Slide 48 text

Blur is slow.

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

class RedactionsController { var redactions = [Redaction]() var image: UIImage? { didSet { if let image = image { ciImage = CIImage(CGImage: image.CGImage) } else { ciImage = nil } } } private var ciImage: CIImage? { didSet { updateImages() } } // ... }

Slide 51

Slide 51 text

private var pixelatedImage: CIImage? private var blurredImage: CIImage? private func updateImages() { if let ciImage = ciImage { pixelatedImage = Redaction.preprocess(ciImage, type: .Pixelate) blurredImage = Redaction.preprocess(ciImage, type: .Blur) } else { pixelatedImage = nil blurredImage = nil } }

Slide 52

Slide 52 text

func process() -> CIImage? { if let ciImage = ciImage { var outputImage = ciImage if redactions.count > 0 { let chain = ChainFilter() chain.inputImage = ciImage chain.inputFilters = redactions.map({ $0.filter(ciImage, preprocessor: self.preprocess) }) outputImage = chain.outputImage! } return outputImage.imageByCroppingToRect(ciImage.extent()) } return nil }

Slide 53

Slide 53 text

private func preprocess(image: CIImage, type: RedactionType) -> CIImage { switch type { case .Pixelate: return pixelatedImage! case .Blur: return blurredImage! default: return Redaction.preprocess(image, type: type) } }

Slide 54

Slide 54 text

Better than inheritance.

Slide 55

Slide 55 text

FUNCTIONS EVERYWHERE

Slide 56

Slide 56 text

Value Types

Slide 57

Slide 57 text

private let redactionsController = RedactionsController() public var redactions: [Redaction] { get { return redactionsController.redactions } set { redactionsController.redactions = newValue updateRedactions() } }

Slide 58

Slide 58 text

Automatic Rendering

Slide 59

Slide 59 text

func updateRedactions() { image = redactionsController.process() updateSelections() }

Slide 60

Slide 60 text

class CoreImageView: OpenGLView { var image: CIImage? { didSet { draw() } } // ... }

Slide 61

Slide 61 text

Selection

Slide 62

Slide 62 text

var selectedUUIDs = Set() { didSet { updateSelections() } }

Slide 63

Slide 63 text

func isSelected(redaction: Redaction) -> Bool { return selectedUUIDs.contains(redaction.UUID) }

Slide 64

Slide 64 text

func select(redaction: Redaction) { if isSelected(redaction) { return } selectedUUIDs.insert(redaction.UUID) CATransaction.begin() CATransaction.setDisableActions(true) let box = BoundingBoxLayer() boundingBoxes[redaction.UUID] = box let layer: CALayer? = self.layer layer?.addSublayer(box) updateSelections() CATransaction.commit() }

Slide 65

Slide 65 text

Core Animation

Slide 66

Slide 66 text

var selectedRedactions: [Redaction] { var selected = [Redaction]() let allUUIDs = redactions.map({ $0.UUID }) for UUID in selectedUUIDs { if let index = find(allUUIDs, UUID) { selected.append(redactions[index]) } } return selected }

Slide 67

Slide 67 text

func updateSelections() { CATransaction.begin() CATransaction.setDisableActions(true) for redaction in selectedRedactions { if let layer = boundingBoxes[redaction.UUID] { var rect = redaction.rectForBounds(imageRect) rect.inset(dx: -2, dy: -2) layer.frame = rect } } CATransaction.commit() NSNotificationCenter.defaultCenter().postNotificationName(... }

Slide 68

Slide 68 text

Dragging

Slide 69

Slide 69 text

enum DraggingMode { case Creating(String) case Moving(String, CGRect, CGPoint) } var draggingMode: DraggingMode?

Slide 70

Slide 70 text

public func drag(#point: CGPoint, state: GestureRecognizerState) { if image == nil { return } let point = convertPointToUnits(point) // Begin dragging if state == .Began { deselectAll() // Start moving if let redaction = hitTestRedaction(point) { draggingMode = .Moving(redaction.UUID, redaction.rect, point) select(redaction) } // Start creating else { let redaction = Redaction(type: mode, rect: CGRect(origin: point, size: .zeroSize)) draggingMode = .Creating(redaction.UUID) redactions.append(redaction) select(redaction) } }

Slide 71

Slide 71 text

// Continue dragging if let draggingMode = draggingMode { switch draggingMode { case let .Creating(UUID): // Update size based on origin and current point case let .Moving(UUID, rect, startPoint): // Update rect based on original rect and start point } } // End dragging if state == .Ended { draggingMode = nil } }

Slide 72

Slide 72 text

Clever Things Functional Things

Slide 73

Slide 73 text

Clever Things

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

#if os(iOS) import UIKit.UIFont public typealias FontType = UIFont #else import AppKit.NSFont public typealias FontType = NSFont #endif public typealias Font = FontType

Slide 77

Slide 77 text

#if os(iOS) import UIKit.UIColor public typealias ColorType = UIColor #else import AppKit.NSColor public typealias ColorType = NSColor extension NSColor { public convenience init(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) { self.init(SRGBRed: red, green: green, blue: blue, alpha: alpha) } } #endif public typealias Color = ColorType

Slide 78

Slide 78 text

#if os(iOS) import UIKit.UIImage public typealias ImageType = UIImage #else import AppKit.NSImage public typealias ImageType = NSImage extension NSImage { public var CGImage: CGImageRef! { return CGImageForProposedRect(nil, context: nil, hints: nil)?.takeUnretainedValue() } // Optional to match UIImage public convenience init?(CGImage cgImage: CGImageRef) { self.init(CGImage: cgImage, size: CGSizeZero) } } #endif public typealias Image = ImageType

Slide 79

Slide 79 text

#if os(iOS) extension Image { public convenience init?(named name: String, inBundle bundle: NSBundle?) { self.init(named: name, inBundle: bundle, compatibleWithTraitCollection: nil) } } #endif // Mac version implemented in Objective-C

Slide 80

Slide 80 text

public class View: ViewType { #if os(iOS) public var wantsLayer: Bool { set { // Do nothing } get { return true } } #else public override func viewDidMoveToWindow() { didMoveToWindow() } public func didMoveToWindow() { super.viewDidMoveToWindow() }

Slide 81

Slide 81 text

ContentMode EdgeInsets Screen Window

Slide 82

Slide 82 text

2014.funswiftconf.com/speakers/andy.html 2014.funswiftconf.com/speakers/justin.html objc.io/books

Slide 83

Slide 83 text

Thanks

Slide 84

Slide 84 text

No content