Lecture 6 Interface Builder, Protocols and Delegation, Gestures

Interface Builder

  • @IBDesignable
  • @IBInspectable

The Happiness MVC’s Model

Extensions

  • You can add methods and properties to a class (even if you don’t have the source).
  • You can’t re-implement methods or properties that are already there.
  • The properties you add can have no storage associated with them.
  • This feature is easily abused.

Protocols and Delegation

A way to express an API minimally

  • Instead of forcing the caller to pass a class/struct, we can ask for specifically what we want.
  • We just specify the properties and methods needed.

A protocol is a TYPE just like any other type, except

  • It has no storage or implementation associated with it.
  • Any storage or implementation required to implement the protocol is in an implementing type.
  • An implementing type can be any class, struct or enum.
  • Otherwise, a protocol can be used as a type to declare variables, as a function parameter, etc.

There are three aspects to a protocol

  1. the protocol declaration
  2. the declaration where a class, struct or enum says that it implements a protocol
  3. the actual implementation of the protocol in said class, struct or enum

Declaration of the protocol itself

  • Anyone that implements SomeProtocol must also implement InheritedProtocol1 and 2.
  • You must specify whether a property is get only or both get and set.
  • Any functions that are expected to mutate the receiver should be marked mutating (unless you are going to restrict your protocol to class implementers only with class keyword).
  • You can even specify that implementers must implement a given initializer.

How an implementer says “I implement that protocol”

  • Claims of conformance to protocols are listed after the superclass for a class.
  • obviously, enums and structs would not have the superclass part.
  • Any number of protocols can be implemented by a give class, struct or enum.
  • In a class, inits must be marked required.
  • You are allowed to add protocol conformance via an extension.

Using protocols like the type that they are!

Gestures

Leave a Reply

Your email address will not be published. Required fields are marked *