66
@NatashaTheRobot

Practical Protocol-Oriented-Programming

Embed Size (px)

Citation preview

Page 1: Practical Protocol-Oriented-Programming

@NatashaTheRobot

Page 2: Practical Protocol-Oriented-Programming

Protocol-Oriented Programming in Swift Dave Abrahams Professor of Blowing-Your-Mind

Page 3: Practical Protocol-Oriented-Programming

–- Professor of Blowing-Your-Mind

"Swift Is a Protocol-Oriented Programming Language"

Page 4: Practical Protocol-Oriented-Programming
Page 5: Practical Protocol-Oriented-Programming

Practical POP

• View

• (UITable)ViewController

• Networking

Page 6: Practical Protocol-Oriented-Programming

POP Views

Page 7: Practical Protocol-Oriented-Programming
Page 8: Practical Protocol-Oriented-Programming
Page 9: Practical Protocol-Oriented-Programming

// FoodImageView.swift

import UIKit

class FoodImageView: UIImageView { func shake() { let animation = CABasicAnimation(keyPath: "position") animation.duration = 0.05 animation.repeatCount = 5 animation.autoreverses = true animation.fromValue = NSValue(CGPoint: CGPointMake(self.center.x - 4.0, self.center.y)) animation.toValue = NSValue(CGPoint: CGPointMake(self.center.x + 4.0, self.center.y)) layer.addAnimation(animation, forKey: "position") } }

Page 10: Practical Protocol-Oriented-Programming

// ViewController.swift

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var foodImageView: FoodImageView! @IBAction func onShakeButtonTap(sender: AnyObject) { foodImageView.shake() } }

Page 11: Practical Protocol-Oriented-Programming

💃💃💃

Page 12: Practical Protocol-Oriented-Programming
Page 13: Practical Protocol-Oriented-Programming

// ShakeableButton.swift

import UIKit

class ActionButton: UIButton {

func shake() { let animation = CABasicAnimation(keyPath: "position") animation.duration = 0.05 animation.repeatCount = 5 animation.autoreverses = true animation.fromValue = NSValue(CGPoint: CGPointMake(self.center.x - 4.0, self.center.y)) animation.toValue = NSValue(CGPoint: CGPointMake(self.center.x + 4.0, self.center.y)) layer.addAnimation(animation, forKey: "position") }

}

Page 14: Practical Protocol-Oriented-Programming

// ViewController.swift

class ViewController: UIViewController {

@IBOutlet weak var foodImageView: FoodImageView! @IBOutlet weak var actionButton: ActionButton! @IBAction func onShakeButtonTap(sender: AnyObject) { foodImageView.shake() actionButton.shake() } }

Page 15: Practical Protocol-Oriented-Programming

🤔

Page 16: Practical Protocol-Oriented-Programming

// UIViewExtension.swift

import UIKit

extension UIView { func shake() { let animation = CABasicAnimation(keyPath: "position") animation.duration = 0.05 animation.repeatCount = 5 animation.autoreverses = true animation.fromValue = NSValue(CGPoint: CGPointMake(self.center.x - 4.0, self.center.y)) animation.toValue = NSValue(CGPoint: CGPointMake(self.center.x + 4.0, self.center.y)) layer.addAnimation(animation, forKey: "position") } }

Page 17: Practical Protocol-Oriented-Programming

class FoodImageView: UIImageView { // other customization here }

class ActionButton: UIButton { // other customization here }

class ViewController: UIViewController {

@IBOutlet weak var foodImageView: FoodImageView! @IBOutlet weak var actionButton: ActionButton! @IBAction func onShakeButtonTap(sender: AnyObject) { foodImageView.shake() actionButton.shake() } }

Page 18: Practical Protocol-Oriented-Programming
Page 19: Practical Protocol-Oriented-Programming

// Shakeable.swift

import UIKit

protocol Shakeable { }

extension Shakeable where Self: UIView { func shake() { // implementation code } }

Page 20: Practical Protocol-Oriented-Programming

class FoodImageView: UIImageView, Shakeable {

}

class ActionButton: UIButton, Shakeable {

}

Page 21: Practical Protocol-Oriented-Programming

class FoodImageView: UIImageView, Shakeable, Dimmable {

}

Page 22: Practical Protocol-Oriented-Programming

class FoodImageView: UIImageView, Dimmable {

}

Page 23: Practical Protocol-Oriented-Programming

Transparent View Controllers and Dim Backgrounds

totem.training

Page 24: Practical Protocol-Oriented-Programming

👯👯👯👯👯

Page 25: Practical Protocol-Oriented-Programming

POP (UITable)ViewControllers

Page 26: Practical Protocol-Oriented-Programming
Page 27: Practical Protocol-Oriented-Programming

// FoodLaLaViewController

override func viewDidLoad() { super.viewDidLoad() let foodCellNib = UINib(nibName: "FoodTableViewCell", bundle: nil) tableView.registerNib(foodCellNib, forCellReuseIdentifier: "FoodTableViewCell") }

Page 28: Practical Protocol-Oriented-Programming

let foodCellNib = UINib(nibName: String(FoodTableViewCell), bundle: nil) tableView.registerNib(foodCellNib, forCellReuseIdentifier: String(FoodTableViewCell))

Page 29: Practical Protocol-Oriented-Programming

protocol ReusableView: class {}

extension ReusableView where Self: UIView {

static var reuseIdentifier: String { return String(self) }

}

Page 30: Practical Protocol-Oriented-Programming

extension UITableViewCell: ReusableView { }

FoodTableViewCell.reuseIdentifier // FoodTableViewCell

Page 31: Practical Protocol-Oriented-Programming

let foodCellNib = UINib(nibName: "FoodTableViewCell", bundle: nil) tableView.registerNib(foodCellNib, forCellReuseIdentifier: FoodTableViewCell.reuseIdentifier)

Page 32: Practical Protocol-Oriented-Programming

protocol NibLoadableView: class { }

extension NibLoadableView where Self: UIView {

static var nibName: String { return String(self) }

}

Page 33: Practical Protocol-Oriented-Programming

extension FoodTableViewCell: NibLoadableView { }

FoodTableViewCell.nibName // "FoodTableViewCell"

Page 34: Practical Protocol-Oriented-Programming

let foodCellNib = UINib(nibName: FoodTableViewCell.nibName, bundle: nil) tableView.registerNib(foodCellNib, forCellReuseIdentifier: FoodTableViewCell.reuseIdentifier)

Page 35: Practical Protocol-Oriented-Programming

extension UITableView { func register<T: UITableViewCell where T: ReusableView, T: NibLoadableView>(_: T.Type) {

let nib = UINib(nibName: T.nibName, bundle: nil) registerNib(nib, forCellReuseIdentifier: T.reuseIdentifier) } }

Page 36: Practical Protocol-Oriented-Programming

let foodCellNib = UINib(nibName: "FoodTableViewCell", bundle: nil) tableView.registerNib(foodCellNib, forCellReuseIdentifier: "FoodTableViewCell")

Page 37: Practical Protocol-Oriented-Programming

tableView.register(FoodTableViewCell)

Page 38: Practical Protocol-Oriented-Programming

extension UITableView { func dequeueReusableCell<T: UITableViewCell where T: ReusableView>(forIndexPath indexPath: NSIndexPath) -> T { guard let cell = dequeueReusableCellWithIdentifier(T.reuseIdentifier, forIndexPath: indexPath) as? T else { fatalError("Could not dequeue cell with identifier: \(T.reuseIdentifier)") } return cell } }

Page 39: Practical Protocol-Oriented-Programming

guard let cell = tableView.dequeueReusableCellWithIdentifier(“FoodTableViewCell", forIndexPath: indexPath) as? FoodTableViewCell else { fatalError("Could not dequeue cell with identifier: FoodTableViewCell") }

Page 40: Practical Protocol-Oriented-Programming

let cell = tableView.dequeueReusableCell(forIndexPath: indexPath) as FoodTableViewCell

Page 41: Practical Protocol-Oriented-Programming

if indexPath.row == 0 { return tableView.dequeueReusableCell(forIndexPath: indexPath) as DesertTableViewCell } return tableView.dequeueReusableCell(forIndexPath: indexPath) as FoodTableViewCell

Page 42: Practical Protocol-Oriented-Programming

iOS Cell Registration & Reusing with Swift Protocol Extensions and

Genericsmedium.com/@gonzalezreal

Page 43: Practical Protocol-Oriented-Programming

Protocol-Oriented Segue Identifiers in Swift

natashatherobot.com

Page 44: Practical Protocol-Oriented-Programming
Page 45: Practical Protocol-Oriented-Programming

POP Networking

Page 46: Practical Protocol-Oriented-Programming

struct FoodService { func get(completionHandler: Result<[Food]> -> Void) { // make asynchronous API call // and return appropriate result } }

Page 47: Practical Protocol-Oriented-Programming

enum Result<T> { case Success(T) case Failure(ErrorType) }

Page 48: Practical Protocol-Oriented-Programming

struct FoodService { func get(completionHandler: Result<[Food]> -> Void) { // make asynchronous API call // and return appropriate result } }

Page 49: Practical Protocol-Oriented-Programming

// FoodLaLaViewController

var dataSource = [Food]() { didSet { tableView.reloadData() } }

override func viewDidLoad() { super.viewDidLoad() getFood() }

private func getFood() { FoodService().getFood() { [weak self] result in switch result { case .Success(let food): self?.dataSource = food case .Failure(let error): self?.showError(error) } } }

Page 50: Practical Protocol-Oriented-Programming

View Controller Tests?!!! 😱

Page 51: Practical Protocol-Oriented-Programming

// FoodLaLaViewController

var dataSource = [Food]() { didSet { tableView.reloadData() } }

override func viewDidLoad() { super.viewDidLoad() getFood() }

private func getFood() { FoodService().getFood() { [weak self] result in switch result { case .Success(let food): self?.dataSource = food case .Failure(let error): self?.showError(error) } } }

Page 52: Practical Protocol-Oriented-Programming

// FoodLaLaViewController

func getFood(fromService service: FoodService) {

service.getFood() { [weak self] result in // handle result } }

Page 53: Practical Protocol-Oriented-Programming

// FoodLaLaViewControllerTests

func testFetchFood() { viewController.getFood(fromService: FoodService()) // 🤔 now what? }

Page 54: Practical Protocol-Oriented-Programming

struct FoodService { func get(completionHandler: Result<[Food]> -> Void) { // make asynchronous API call // and return appropriate result } }

Page 55: Practical Protocol-Oriented-Programming

protocol Gettable { associatedtype T func get(completionHandler: Result<T> -> Void) }

Page 56: Practical Protocol-Oriented-Programming

struct FoodService: Gettable { func get(completionHandler: Result<[Food]> -> Void) { // make asynchronous API call // and return appropriate result } }

Page 57: Practical Protocol-Oriented-Programming

// FoodLaLaViewController

override func viewDidLoad() { super.viewDidLoad() getFood(fromService: FoodService()) }

func getFood<S: Gettable where S.T == [Food]>(fromService service: S) { service.get() { [weak self] result in switch result { case .Success(let food): self?.dataSource = food case .Failure(let error): self?.showError(error) } } }

Page 58: Practical Protocol-Oriented-Programming

// FoodLaLaViewControllerTests

class Fake_FoodService: Gettable { var getWasCalled = false func get(completionHandler: Result<[Food]> -> Void) { getWasCalled = true completionHandler(Result.Success(food)) } }

Page 59: Practical Protocol-Oriented-Programming

// FoodLaLaViewControllerTests

func testFetchFood() { let fakeFoodService = Fake_FoodService() viewController.getFood(fromService: fakeFoodService) XCTAssertTrue(fakeFoodService.getWasCalled) XCTAssertEqual(viewController.dataSource.count, food.count) XCTAssertEqual(viewController.dataSource, food) }

Page 60: Practical Protocol-Oriented-Programming

Update: View Controller Data Injection with Storyboards and

Segues in Swiftnatashatherobot.com

Page 61: Practical Protocol-Oriented-Programming

Protocols with Associated TypesAlexis Gallagher

2015.funswiftconf.com

Page 62: Practical Protocol-Oriented-Programming

😎😎😎

Page 63: Practical Protocol-Oriented-Programming

Practical POP

• View

• (UITable)ViewController

• Networking

Page 64: Practical Protocol-Oriented-Programming

Beyond Crusty: Real-World Protocols

Rob Napier thedotpost.com

Page 65: Practical Protocol-Oriented-Programming

Blending Cultures: The Best of Functional, Protocol-Oriented, and

Object-Oriented ProgrammingDaniel Steinberg

realm.io

Page 66: Practical Protocol-Oriented-Programming

@NatashaTheRobot