3 Miscellaneous topic! Extensions You can add methods and properties to a class (even if you don t have the source). There are some restrictions You can t re-implement methods or properties that are already there (only add new ones). The properties you add can have no storage associated with them. This feature is easily abused It should be used to add clarity to readability not obfuscation! Don t use it as a substitute for good object-oriented design technique. Best used (at least for beginners) for very small, well-contained helper functions. Can actually be used well to organize code but requires architectural commitment. When in doubt (for now), don t do it.

4 Protocols 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 (what properties and methods are in the protocol) 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

7 Protocols Declaration of the protocol itself protocol SomeProtocol : InheritedProtocol1, InheritedProtocol2 { var someproperty: Int { get set func amethod(arg1: Double, anotherargument: String) -> SomeType mutating func changeit()! init(arg: Type) Anyone that implements SomeProtocol must also implement InheritedProtocol1 and 2 You must specify whether a property is get only or both get and set

8 Protocols Declaration of the protocol itself protocol SomeProtocol : InheritedProtocol1, InheritedProtocol2 { var someproperty: Int { get set func amethod(arg1: Double, anotherargument: String) -> SomeType mutating func changeit()! init(arg: Type) 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

9 Protocols Declaration of the protocol itself protocol SomeProtocol : class, InheritedProtocol1, InheritedProtocol2 { var someproperty: Int { get set func amethod(arg1: Double, anotherargument: String) -> SomeType mutating func changeit()! init(arg: Type) 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)

10 Protocols Declaration of the protocol itself protocol SomeProtocol : InheritedProtocol1, InheritedProtocol2 { var someproperty: Int { get set func amethod(arg1: Double, anotherargument: String) -> SomeType mutating func changeit()! init(arg: Type) 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

11 How an implementer says I implement that protocol class SomeClass : SuperclassOfSomeClass, SomeProtocol, AnotherProtocol {! // implementation of SomeClass here! // w hich must include all the properties and methods in SomeProtocol & AnotherProtocol Protocols Claims of conformance to protocols are listed after the superclass for a class

12 Protocols How an implementer says I implement that protocol enum SomeEnum : SomeProtocol, AnotherProtocol {! // implementation of SomeEnum here! // w hich must include all the properties and methods in SomeProtocol & AnotherProtocol Claims of conformance to protocols are listed after the superclass for a class Obviously, enums and structs would not have the superclass part

13 Protocols How an implementer says I implement that protocol struct SomeStruct : SomeProtocol, AnotherProtocol {! // implementation of SomeStruct here! // w hich must include all the properties and methods in SomeProtocol & AnotherProtocol Claims of conformance to protocols are listed after the superclass for a class Obviously, enums and structs would not have the superclass part

14 Protocols How an implementer says I implement that protocol struct SomeStruct : SomeProtocol, AnotherProtocol {! // implementation of SomeStruct here! // w hich must include all the properties and methods in SomeProtocol & AnotherProtocol 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 given class, struct or enum

15 How an implementer says I implement that protocol class SomeClass : SuperclassOfSomeClass, SomeProtocol, AnotherProtocol {! // implementation of SomeClass here, including! required init( ) Protocols 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 given class, struct or enum In a class, inits must be marked required (or otherwise a subclass might not conform)

16 How an implementer says I implement that protocol extension Something : SomeProtocol {! // implementation of SomeProtocol here! // no stored properties though! Protocols 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 given class, struct or enum In a class, inits must be marked required (or otherwise a subclass might not conform) You are allowed to add protocol conformance via an extension

18 Delegation A very important use of protocols It s how we can implement blind communication between a View and its Controller should will did Controller data at count delegate data source View

19 Delegation A very important use of protocols It s how we can implement blind communication between a View and its Controller How it plays out 1. Create a delegation protocol (defines what the View wants the Controller to take care of) 2. Create a delegate property in the View whose type is that delegation protocol 3. Use the delegate property in the View to get/do things it can t own or control 4. Controller declares that it implements the protocol 5. Controller sets self as the delegate of the View by setting the property in #2 above 6. Implement the protocol in the Controller Now the View is hooked up to the Controller But the View still has no idea what the Controller is, so the View remains generic/reusable

20 Demo Let s see FaceView delegate its data That way FaceView can stay generic It won t be tied to HappinessViewController, so it can be used by other Controllers Since it s doing this to get its data (its smiliness), we ll call our delegate property datasource

21 Gestures We ve seen how to draw in a UIView, how do we get touches? We can get notified of the raw touch events (touch down, moved, up, etc.) Or we can react to certain, predefined gestures. The latter is the way to go! Gestures are recognized by instances of UIGestureRecognizer The base class is abstract. We only actually use concrete subclasses to recognize. There are two sides to using a gesture recognizer 1. Adding a gesture recognizer to a UIView (asking the UIView to recognize that gesture) 2. Providing a method to handle that gesture (not necessarily handled by the UIView) Usually the first is done by a Controller Though occasionally a UIView will do this itself if the gesture is integral to its existence The second is provided either by the UIView or a Controller Depending on the situation. We ll see an example of both in our demo.

22 Gestures Adding a gesture recognizer to a UIView Imagine we wanted a UIView in our Controller s View to recognize a pan weak var pannableview: UIView { didset { let recognizer = UIPanGestureRecognizer(target: self, action: pan: ) pannableview.addgesturerecognizer(recognizer) This is just a normal outlet to the UIView we want to recognize the gesture

23 Adding a gesture recognizer to a UIView Imagine we wanted a UIView in our Controller s View to recognize a pan weak var pannableview: UIView { didset { Gestures let recognizer = UIPanGestureRecognizer(target: self, action: pan: ) pannableview.addgesturerecognizer(recognizer) This is just a normal outlet to the UIView we want to recognize the gesture We use its property observer to get involved when the outlet gets hooked up by ios

24 Adding a gesture recognizer to a UIView Imagine we wanted a UIView in our Controller s View to recognize a pan weak var pannableview: UIView { didset { Gestures let recognizer = UIPanGestureRecognizer(target: self, action: pan: ) pannableview.addgesturerecognizer(recognizer) This is just a normal outlet to the UIView we want to recognize the gesture We use its property observer to get involved when the outlet gets hooked up by ios Here we are creating an instance of a concrete subclass of UIGestureRecognizer (for pans)

25 Adding a gesture recognizer to a UIView Imagine we wanted a UIView in our Controller s View to recognize a pan weak var pannableview: UIView { didset { Gestures let recognizer = UIPanGestureRecognizer(target: self, action: pan: ) pannableview.addgesturerecognizer(recognizer) This is just a normal outlet to the UIView we want to recognize the gesture We use its property observer to get involved when the outlet gets hooked up by ios Here we are creating an instance of a concrete subclass of UIGestureRecognizer (for pans) The target gets notified when the gesture is recognized (in this case, the Controller itself)

26 Adding a gesture recognizer to a UIView Imagine we wanted a UIView in our Controller s View to recognize a pan weak var pannableview: UIView { didset { Gestures let recognizer = UIPanGestureRecognizer(target: self, action: pan: ) pannableview.addgesturerecognizer(recognizer) This is just a normal outlet to the UIView we want to recognize the gesture We use its property observer to get involved when the outlet gets hooked up by ios Here we are creating an instance of a concrete subclass of UIGestureRecognizer (for pans) The target gets notified when the gesture is recognized (in this case, the Controller itself) The action is the method invoked on recognition (the : means it has an argument)

27 Adding a gesture recognizer to a UIView Imagine we wanted a UIView in our Controller s View to recognize a pan weak var pannableview: UIView { didset { Gestures let recognizer = UIPanGestureRecognizer(target: self, action: pan: ) pannableview.addgesturerecognizer(recognizer) This is just a normal outlet to the UIView we want to recognize the gesture We use its property observer to get involved when the outlet gets hooked up by ios Here we are creating an instance of a concrete subclass of UIGestureRecognizer (for pans) The target gets notified when the gesture is recognized (in this case, the Controller itself) The action is the method invoked on recognition (the : means it has an argument) Here we ask the UIView to actually start trying to recognize this gesture in its bounds

28 Adding a gesture recognizer to a UIView Imagine we wanted a UIView in our Controller s View to recognize a pan weak var pannableview: UIView { didset { Gestures let recognizer = UIPanGestureRecognizer(target: self, action: pan: ) pannableview.addgesturerecognizer(recognizer) This is just a normal outlet to the UIView we want to recognize the gesture We use its property observer to get involved when the outlet gets hooked up by ios Here we are creating an instance of a concrete subclass of UIGestureRecognizer (for pans) The target gets notified when the gesture is recognized (in this case, the Controller itself) The action is the method invoked on recognition (the : means it has an argument) Here we ask the UIView to actually start trying to recognize this gesture in its bounds Let s talk about how we implement the handler

29 Gestures A handler for a gesture needs gesture-specific information So each concrete subclass provides special methods for handling that type of gesture For example, UIPanGestureRecognizer provides 3 methods func translationinview(view: UIView) -> CGPoint // cumulative since start of recognition func velocityinview(view: UIView) -> CGPoint // how fast the finger is moving (points/s) func settranslation(translation: CGPoint, inview: UIView) This last one is interesting because it allows you to reset the translation so far By resetting the translation to zero all the time, you end up getting incremental translation The abstract superclass also provides state information var state: UIGestureRecognizerState { get This sits around in.possible until recognition starts For a discrete gesture (e.g. a Swipe), it changes to.recognized (Tap is not a normal discrete) For a continues gesture (e.g. a Pan), it moves from.began thru repeated.changed to.ended It can go to.failed or.cancelled too, so watch out for those!

30 So, given this information, what would the pan handler look like? func pan(gesture: UIPanGestureRecognizer) { switch gesture.state { case.changed: fallthrough case.ended: let translation = gesture.translationinview(pannableview) // update anything that depends on the pan gesture using translation.x and.y gesture.settranslation(cgpointzero, inview: pannableview) default: break Gestures Remember that the action was pan: (if no colon, we would not get the gesture argument)

31 So, given this information, what would the pan handler look like? func pan(gesture: UIPanGestureRecognizer) { switch gesture.state { case.changed: fallthrough case.ended: let translation = gesture.translationinview(pannableview) // update anything that depends on the pan gesture using translation.x and.y gesture.settranslation(cgpointzero, inview: pannableview) default: break Gestures Remember that the action was pan: (if no colon, we would not get the gesture argument) We are only going to do anything when the finger moves or lifts up off the device s surface

32 So, given this information, what would the pan handler look like? func pan(gesture: UIPanGestureRecognizer) { switch gesture.state { case.changed: fallthrough case.ended: let translation = gesture.translationinview(pannableview) // update anything that depends on the pan gesture using translation.x and.y gesture.settranslation(cgpointzero, inview: pannableview) default: break Gestures Remember that the action was pan: (if no colon, we would not get the gesture argument) We are only going to do anything when the finger moves or lifts up off the device s surface fallthrough means execute the code for the next case down

33 So, given this information, what would the pan handler look like? func pan(gesture: UIPanGestureRecognizer) { switch gesture.state { case.changed: fallthrough case.ended: let translation = gesture.translationinview(pannableview) // update anything that depends on the pan gesture using translation.x and.y gesture.settranslation(cgpointzero, inview: pannableview) default: break Gestures Remember that the action was pan: (if no colon, we would not get the gesture argument) We are only going to do anything when the finger moves or lifts up off the device s surface fallthrough means execute the code for the next case down Here we get the location of the pan in the pannableview s coordinate system

34 So, given this information, what would the pan handler look like? func pan(gesture: UIPanGestureRecognizer) { switch gesture.state { case.changed: fallthrough case.ended: let translation = gesture.translationinview(pannableview) // update anything that depends on the pan gesture using translation.x and.y gesture.settranslation(cgpointzero, inview: pannableview) default: break Gestures Remember that the action was pan: (if no colon, we would not get the gesture argument) We are only going to do anything when the finger moves or lifts up off the device s surface fallthrough means execute the code for the next case down Here we get the location of the pan in the pannableview s coordinate system Now we do whatever we want with that information

35 So, given this information, what would the pan handler look like? func pan(gesture: UIPanGestureRecognizer) { switch gesture.state { case.changed: fallthrough case.ended: let translation = gesture.translationinview(pannableview) // update anything that depends on the pan gesture using translation.x and.y gesture.settranslation(cgpointzero, inview: pannableview) default: break Gestures Remember that the action was pan: (if no colon, we would not get the gesture argument) We are only going to do anything when the finger moves or lifts up off the device s surface fallthrough means execute the code for the next case down Here we get the location of the pan in the pannableview s coordinate system Now we do whatever we want with that information By resetting the translation, the next one we get will be how much it moved since this one

37 Demo FaceView Gestures Add a gesture recognizer (pinch) to the FaceView to zoom in and out (control its own scale) Add a gesture recognizer (pan) to the FaceView to control happiness (Model) in the Controller

Mobile Application Programming Swift Classes Swift Objects Classes, structures, and enums are all object types with different defaults in usage Classes are reference types that share the same object when

View Controller Lifecycle View Controllers have a Lifecycle A sequence of messages is sent to them as they progress through it Why does this matter? You very commonly override these methods to do certain

WINDOWS 8 CHEAT SHEET FIVE BIG CHANGES IN WINDOWS 8 Windows 8 looks and behaves much differently from previous versions of Windows. Remember these five big changes, and you ll be well prepared for Windows

CS 251 Intermediate Programming Methods and More Brooke Chenoweth University of New Mexico Spring 2018 Methods An operation that can be performed on an object Has return type and parameters Method with

Chapter 14 Understanding an App s Architecture This chapter examines the structure of an app from a programmer s perspective. It begins with the traditional analogy that an app is like a recipe and then

Windows 10 and Office 2016: Controlling Windows with gestures Controlling Windows with gestures If you use a touchscreen or tablet, you no longer need a mouse or keyboard when working with Windows. Instead

ios Mobile Development Today Protocols How to make id a little bit safer. Blocks Passing a block of code as an argument to a method. Animation Dynamic Animator View property animation Demo Dropit! Protocols

1 WACOM GESTURE GUIDE: USING TOUCH WITH WACOM TABLETS Wacom tablets that support touch are designed to enhance the way you work. Touch enables you to interact with a computer using only your fingertips

Handout Handout Session Overview We re excited to share the exciting new features of Windows 10 and look forward to answering any questions in real time as we learn more about this updated platform together.

Lecture 4: Procedure Specifications 4.1. Introduction In this lecture, we ll look at the role played by specifications of methods. Specifications are the linchpin of team work. It s impossible to delegate

How to Use the Goodreader Application Goodreader version 4.0.1 OPEN Goodreader from the ipad HOME SCREEN. The HOME PAGE opens with a screen split into two parts, Left panel and Right panel. The Left Panel

15-150 Lecture 19: Signatures, Structures, and Type Abstraction Lecture by Dan Licata March 27, 2012 In these lectures, we will discuss the use of the ML module system for structuring large programs. Key

1 Could you make the XNA functions yourself? For the second and especially the third assignment, you need to globally understand what s going on inside the graphics hardware. You will write shaders, which

Kebot: Robot Simulator » Completion of consent forms» Introduction to the research project» Structure of the workshop session» Advantages of taking part» Workshop has been designed for complete novices

Help us make this document better smarttech.com/feedback/170897 SMART Meeting Pro 4.1 software USER S GUIDE Product registration If you register your SMART product, we ll notify you of new features and

CS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal) What is the Metacircular Evaluator? It is the best part

Chapter 6 Class Action In Chapter 1, we explained that Java uses the word class to refer to A set, collection, group, or configuration containing members regarded as having certain attributes or traits

Divide objects in your program into 3 camps. = What your application is (but not how it is displayed) = How your is presented to the user (UI logic) = Your s minions It s all about managing communication

Introduction to Design Patterns First, what s a design pattern? a general reusable solution to a commonly occurring problem within a given context in software design It s not a finished design that can

I. Automated Banking System Case studies: Outline Case Study: Noughts and Crosses II. III. Library Noughts and Crosses Definition of the problem Game played on a 3x3 square board between two players. The

VERSION 1 CHAPTER 6 In this chapter we cover ACTIONS in more depth and show how to easily create additional actions in a script by using a technique known as REFACTORING. The chapter covers two forms of

1 OUTLINE GIS Tools for Hydrology and Hydraulics INTRODUCTION Good afternoon! Welcome and thanks for coming. I once heard GIS described as a high-end Swiss Army knife: lots of tools in one little package

Notes on the 2008 Exam A hastily compiled review of this year s exam. Apologies if there are errors. Please also read notes on previous years exams especially the first set of notes. Question 1 Question

Inheritance In our daily lives, we classify the many things around us. The world has objects like dogs and cars and food and we are familiar with talking about these objects as classes Dogs are animals

DOWNLOADING OFFICE 365 TO YOUR HOME COMPUTER You can download Office 365 to your home computer up to five by using the following link https://portal.office.com/home or by navigating to https://my.lynn.edu/ics

Pointers, Arrays and Parameters This exercise is different from our usual exercises. You don t have so much a problem to solve by creating a program but rather some things to understand about the programming

Validating for Missing Maps Using JOSM This document covers processes for checking data quality in OpenStreetMap, particularly in the context of Humanitarian OpenStreetMap Team and Red Cross Missing Maps

Lecture Transcript While and Do While Statements in C++ Hello and welcome back. In this lecture we are going to look at the while and do...while iteration statements in C++. Here is a quick recap of some

Extracted from: Beginning Mac Programming Develop with Objective-C and Cocoa This PDF file contains pages extracted from Beginning Mac Programming, published by the Pragmatic Bookshelf. For more information

Getting Started Welcome to the official Apple Pro training course for Motion, Apple Computer s revolutionary real-time-design motion graphics application. This book provides a comprehensive guide to designing

ACT SparkNotes Test Prep: Plane Geometry Plane Geometry Plane geometry problems account for 14 questions on the ACT Math Test that s almost a quarter of the questions on the Subject Test If you ve taken

Inheritance & Polymorphism Recap Inheritance & Polymorphism 1 Introduction! Besides composition, another form of reuse is inheritance.! With inheritance, an object can inherit behavior from another object,

CS193E: Temperature Converter Walkthrough The goal of this walkthrough is to give you a fairly step by step path through building a simple Cocoa application. You are encouraged to follow the walkthrough,

My First iphone App (for Xcode version 6.4) 1. Tutorial Overview In this tutorial, you re going to create a very simple application on the iphone or ipod Touch. It has a text field, a label, and a button

Factory Method Pattern Creational Intent» Define an interface for creating an object but lets subclasses decide the specific class to instantiate > Delegate creation to the appropriate subclass Also known

Web Hosting Important features to consider Amount of Storage When choosing your web hosting, one of your primary concerns will obviously be How much data can I store? For most small and medium web sites,

Grand Central Dispatch and NSOperation CSCI 5828: Foundations of Software Engineering Lecture 28 12/03/2015 1 Credit Where Credit Is Due Most of the examples in this lecture were inspired by example code

CPS122 Lecture: Design Patterns Last revised March 20, 2012 Objectives 1. To introduce and illustrate the idea of design patterns 2. To introduce some key design patterns students have used or will use:

Getting to Know Keynote on ipad This guide will give you the basic instruction of how to use the Keynote App on ipad. Get to Know Keynote Step 1 To create new presentations and find the ones you ve worked

Welcome to your NOOK HD Quick Start Guide 1 Charge your NOOK You must charge your NOOK before using it the first time. Plug your NOOK into a wall outlet using the Power Adapter and 30-pin Cable included

Chapter 2 Shorthand for values: variables 2.1 Defining a variable You ve typed a lot of expressions into the computer involving pictures, but every time you need a different picture, you ve needed to find

CSE341, Fall 2011, Lecture 26 Summary Standard Disclaimer: This lecture summary is not necessarily a complete substitute for attending class, reading the associated code, etc. It is designed to be a useful

Java s Implementation of Concurrency, and how to use it in our applications. 1 An application running on a single CPU often appears to perform many tasks at the same time. For example, a streaming audio/video

* This claim chart applies to all Respondents Apple Inc. ("Apple"), AT&T Inc., and AT&T Mobility LLC (together, "AT&T"). The activities of Respondents all involve the same Apple products detailed in this