Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Protocols and The Promised Land Take 2

Protocols and The Promised Land Take 2

try! Swift edition

Michele

March 02, 2016
Tweet

More Decks by Michele

Other Decks in Programming

Transcript

  1. public protocol ViewControllerProtocol { var view: UIView! { get }

    var storyboard: UIStoryboard? { get } init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) func viewDidLoad() // etc } protocol ThemeableViewController: Themeable, ViewControllerProtocol {} var themedViewController: ThemeableViewController Swift-y but hack-y
  2. var themed: Themeable { get { return self.viewController as! Themeable

    } set(newThemeable) { if let themedViewController = newThemeable as? UIViewController{ viewController = themedViewController } } } var viewController: UIViewController init<T where T: Themeable, T: UIViewController>(viewController: T) { self.viewController = viewController } Also swfit-y also hack-y
  3. protocol Holder { typealias Item var items: [Item] { get

    } } class Basket<Thing>: Holder{ var items: [Thing] = [] func add(item: Thing) { items.append(item) } }
  4. class Basket<Thing: Fruit> { var items: [Thing] = [] func

    add(item: Thing) { items.append(item) } }
  5. class Basket<Thing: Fruit> { var items: [Thing] = [] func

    add(item: Thing) { items.append(item) } } class FruitBasket: Basket<Fruit> {} class PeachBasket: FruitBasket<Peach> {}
  6. class Bag<Stuff> { class Basket<Thing: Stuff> { var items: [Thing]

    = [] func add(item: Thing) { items.append(item) } } }