Stanford CS193p. Developing Applications for ios Fall Stanford CS193p. Fall 2013

Similar documents
Objective-C. Stanford CS193p Fall 2013

CS193p Spring 2010 Wednesday, March 31, 2010

Developing Applications for ios

Mobile App Development. ios Platform

MVC. Controller. Model. View. Divide objects in your program into 3 camps. Stanford CS193p Fall 2011

Model-View-Controller Architecture

Review (Basic Objective-C)

Objective-C. Deck.m. Deck.h. Let s look at another class. This one represents a deck of cards. #import <Foundation/Foundation.h> #import "Deck.

iphone App Basics iphone and ipod touch Development Fall 2009 Lecture 5

CS193p Spring 2010 Thursday, April 29, 2010

ITP 342 Mobile App Dev. Fundamentals

Mobile Application Development

Developing Applications for ios

Stanford CS193p. Developing Applications for ios. Spring Stanford CS193p. Spring 2012

Review. iphone Application Programming Lecture 2: Objective-C, Cocoa. History. Objective-C. Device restrictions. Interaction paradigm changes

CS193P - Lecture 3. iphone Application Development. Custom Classes Object Lifecycle Autorelease Properties

Cocoa. Last Week... Music 3SI: Introduction to Audio/Multimedia App. Programming. Today... Why Cocoa? Wikipedia - Cocoa

Introductory ios Development

ITP 342 Advanced Mobile App Dev. Memory

JAVA An overview for C++ programmers

ios: Objective-C Primer

Questions. Exams: no. Get by without own Mac? Why ios? ios vs Android restrictions. Selling in App store how hard to publish? Future of Objective-C?

Stanford CS193p. Developing Applications for ios. Spring CS193p. Spring 2016

iphone Programming Patrick H. Madden SUNY Binghamton Computer Science Department

OVERVIEW. Why learn ios programming? Share first-hand experience. Identify platform differences. Identify similarities with.net

Design Phase. Create a class Person. Determine the superclass. NSObject (in this case)

IPHONE DEVELOPMENT. Getting Started with the iphone SDK

When we re first learning Cocoa (or Java, or Qt, or any other application framework),

Announcements. Today s Topics

CS 47. Beginning iphone Application Development

Intro to Native ios Development. Dave Koziol Arbormoon Software, Inc.

View Controller Lifecycle

Praktikum Entwicklung von Mediensystemen mit

Assignment II: Foundation Calculator

Announcement. Final Project Proposal Presentations and Updates

Objective-C and COCOA Applications

Software System/Design & Architecture. Eng.Muhammad Fahad Khan Assistant Professor Department of Software Engineering

Mobile Application Development

Review. iphone Application Programming Lecture 2: Objective-C, Cocoa. History. Objective-C. Device restrictions. Interaction paradigm changes

Stanford CS193p. Developing Applications for ios. Fall CS193p. Fall

Stanford CS193p. Developing Applications for ios. Fall Stanford CS193p. Fall 2011

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

CS193P: HelloPoly Walkthrough

Stanford CS193p. Developing Applications for ios. Winter CS193p. Winter 2017

Topics in Mobile Computing

Assignment I Walkthrough

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

Mobile Application Programming. Objective-C Classes

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Objective-C ICT/7421ICTNathan. René Hexel. School of Information and Communication Technology Griffith University.

Administration. Objects and Arrays. Objects. Agenda. What is an Object? What is a Class?

ios Mobile Development

CS121/IS223. Object Reference Variables. Dr Olly Gotel

Intro. Scheme Basics. scm> 5 5. scm>

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

X Review. Mac OS X Roots: NeXT. BWS Available for virtually every OS

Principles of Programming Languages. Objective-C. Joris Kluivers

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

Lecture Transcript While and Do While Statements in C++

Type Checking and Type Equality

Grade Weights. Language Design and Overview of COOL. CS143 Lecture 2. Programming Language Economics 101. Lecture Outline

1 Getting started with Processing

Maxime Defauw. Learning Swift

ios Application Development Course Details

MITOCW watch?v=flgjisf3l78

CS193E Lecture #3 Categories and Protocols Cocoa Memory Management

Object-Oriented Programming in Objective-C

Stanford CS193p. Developing Applications for ios. Fall CS193p. Fall

CS1004: Intro to CS in Java, Spring 2005

6. Pointers, Structs, and Arrays. 1. Juli 2011

Operational Semantics. One-Slide Summary. Lecture Outline

6. Pointers, Structs, and Arrays. March 14 & 15, 2011

Assignment II: Calculator Brain

Stanford CS193p. Developing Applications for iphone 4, ipod Touch, & ipad Fall Stanford CS193p Fall 2010

Assignment III: Graphing Calculator

CS193E Lecture 7. Document-based Applications NSTableView Key-Value Coding

Chapter 1 Getting Started

Beginning Mac Programming

ITP 342 Mobile App Dev. Fundamentals

Assignment I: Concentration

CSCI 447 Operating Systems Filip Jagodzinski

16 Multiple Inheritance and Extending ADTs

Stanford CS193p. Developing Applications for iphone 4, ipod Touch, & ipad Spring Stanford CS193p Spring 2011

Stanford CS193p. Developing Applications for ios. Winter CS193p. Winter 2017

CS211 Computers and Programming Matthew Harris and Alexa Sharp July 9, Boggle

Accessibility on ios. Developing for everyone. Frameworks #WWDC14. Session 210 Clare Kasemset ios Accessibility

Xcode and Swift CS 4720 Mobile Application Development

CS 31: Introduction to Computer Systems : Threads & Synchronization April 16-18, 2019

HCIM SUMMER WORKSHOP Introduction to C#

Praktikum Entwicklung von Mediensystemen mit ios

CMSC201 Computer Science I for Majors

Adding content to your Blackboard 9.1 class

CS 32. Lecture 2: objects good?

Welcome to CS193E. Mac OS X Cocoa Programming. James Dempsey Paul Marcos

Types, Expressions, and States

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Lecture 1 Introduction to Android. App Development for Mobile Devices. App Development for Mobile Devices. Announcement.

Introductory ios Development

Administrative Notes January 25, 2018

MPATE-GE 2618: C Programming for Music Technology. Syllabus

Transcription:

Developing Applications for ios -14

Today What is this class all about? Description Prerequisites Homework / Final Project ios Overview What s in ios? MVC Object-Oriented Design Concept Objective C (Time Permitting) New language! Basic concepts only for today.

What will I learn in this course? How to build cool apps Easy to build even very complex applications Result lives in your pocket or backpack! Very easy to distribute your application through the AppStore Vibrant development community Real-life Object-Oriented Programming The heart of Cocoa Touch is 100% object-oriented Application of MVC design model Many computer science concepts applied in a commercial development platform: Databases, Graphics, Multimedia, Multithreading, Animation, Networking, and much, much more! Numerous students have gone on to sell products on the AppStore

Prerequisites Most Important Prereq! Object-Oriented Programming CS106A&B (or X) required CS107 or CS108 or CS110 required (or equivalent for non-stanford undergrad) Object-Oriented Terms Class (description/template for an object) Instance (manifestation of a class) Message (sent to object to make it act) Method (code invoked by a Message) Instance Variable (object-specific storage) Superclass/Subclass (Inheritance) You should know these terms! If you are not very comfortable with all of these, this might not be the class for you! Programming Experience This is an upper-level CS course. If you have never written a program where you had to design and implement more than a handful of classes, this will be a big step up in difficulty for you.

Assignments Weekly Homework 6 weekly (approximately) assignments Individual work only Required Tasks and Evaluation criteria Final Project 3 weeks to work on it Individual work only Keynote presentation required (2 mins or so)

What s in ios? Core OS OSX Kernel Mach 3.0 BSD Sockets Security Power Management Keychain Access Certificates File System Bonjour

What s in ios? Core Services Collections Address Book Networking File Access SQLite Core Location Net Services Threading Preferences URL Utilities

What s in ios? Media Core Audio OpenAL Audio Mixing Audio Recording Video Playback JPEG, PNG, TIFF PDF Quartz (2D) Core Animation OpenGL ES

What s in ios? Cocoa Touch Multi-Touch Core Motion View Hierarchy Localization Controls Alerts Web View Map Kit Image Picker Camera

Platform Components Tools Xcode 5 Instruments Language [display settextcolor:[uicolor blackcolor]]; Frameworks Foundation Core Data Map Kit UIKit Core Motion Design Strategies MVC

MVC Controller Model View Divide objects in your program into 3 camps.

MVC Controller Model View Model = What your application is (but not how it is displayed)

MVC Controller Model View Controller = How your Model is presented to the user (UI logic)

MVC Controller Model View View = Your Controller s minions

MVC Controller Model View It s all about managing communication between camps

MVC Controller Model View Controllers can always talk directly to their Model.

MVC Controller Model outlet View Controllers can also talk directly to their View.

MVC Controller Model outlet View The Model and View should never speak to each other.

MVC Controller Model outlet? View Can the View speak to its Controller?

MVC Controller Model outlet View Sort of. Communication is blind and structured.

MVC target Controller Model outlet View The Controller can drop a target on itself.

MVC target Controller Model action outlet View Then hand out an action to the View.

MVC target Controller outlet action Model View The View sends the action when things happen in the UI.

MVC target Controller outlet action should Model will did View Sometimes the View needs to synchronize with the Controller.

MVC target should will did Controller outlet g le de e at Model action View The Controller sets itself as the View s delegate.

MVC target should will did Controller outlet g le de e at Model action View The delegate is set via a protocol (i.e. it s blind to class).

MVC target should will did Controller outlet g le de e at Model action View Views do not own the data they display.

MVC target should will did Controller outlet g le de e at Model data at action View count So, if needed, they have a protocol to acquire it.

MVC target should will did Controller data at count rc e e at so u g le Model de da ta outlet action View Controllers are almost always that data source (not Model!).

MVC target should will did Controller data at count rc e e at so u g le Model de da ta outlet action View Controllers interpret/format Model information for the View.

MVC target should will did Controller so u rc e e at da ta outlet g le Model count de? data at action View Can the Model talk directly to the Controller?

MVC target should will did Controller data at count rc e e at so u g le Model de da ta outlet action View No. The Model is (should be) UI independent.

MVC target should will did Controller data at count rc e e at so u g le Model de da ta outlet action View So what if the Model has information to update or something?

MVC target should will did Controller data at rc e e at so u g le Model da ta outlet de Notification & KVO count action View It uses a radio station -like broadcast mechanism.

MVC target should will did Controller data at rc e e at so u g le Model da ta outlet de Notification & KVO count action View Controllers (or other Model) tune in to interesting stuff.

MVC target should will did Controller data at rc e e at so u g le Model da ta outlet de Notification & KVO count action View A View might tune in, but probably not to a Model s station.

MVC target should will did Controller data at rc e e at so u g le Model da ta outlet de Notification & KVO count action View Now combine MVC groups to make complicated programs...

MVCs working together

MVCs not working together

Objective-C New language to learn! Strict superset of C Adds syntax for classes, methods, etc. A few things to think differently about (e.g. properties, dynamic binding) Most important concept to understand today: Properties Usually we do not access instance variables directly in Objective-C. Instead, we use properties. A property is just the combination of a getter method and a setter method in a class. The getter (usually) has the name of the property (e.g. myvalue ) The setter s name is set plus capitalized property name (e.g. setmyvalue: ) (To make this look nice, we always use a lowercase letter as the first letter of a property name.) We just call the setter to store the value we want and the getter to get it. Simple. This is just your first glimpse of this language! We ll go much more into the details next week. Don t get too freaked out by the syntax at this point.

Card.h Objective-C Card.m Public Declarations Private Implementation

Card.h Objective-C Card.m Its superclass. @interface Card : NSObject The name of this class. NSObject is the root class from which pretty much all ios classes inherit (including the classes you author yourself). Don t forget this!

Card.h Objective-C Card.m @interface Card : NSObject @implementation Card Note, superclass is not specified here.

Card.h #import <Foundation/NSObject.h> Superclass s header file. Objective-C Card.m @interface Card : NSObject @implementation Card

Card.h #import <Foundation/Foundation.h> Objective-C Card.m @interface Card : NSObject If the superclass is in ios itself, we import the entire @implementation Card framework that includes the superclass. In this case, Foundation, which contains basic non-ui objects like NSObject.

Card.h @import Foundation; Objective-C Card.m In fact, in ios 7 (only), there is special syntax for importing an entire framework called @import. @interface Card : NSObject @implementation Card

Card.h #import <Foundation/Foundation.h> Objective-C Card.m @interface Card : NSObject @implementation Card However, the old framework importing syntax is backwards-compatible in ios 7.

Card.h #import <Foundation/Foundation.h> Objective-C #import "Card.h" Card.m Our own header file must be imported into our implementation file. @interface Card : NSObject @implementation Card

Card.h #import <Foundation/Foundation.h> Objective-C #import "Card.h" Card.m @interface Card() Private declarations can go here. @interface Card : NSObject @implementation Card

Card.h #import <Foundation/Foundation.h> @interface Card : NSObject @property (strong) NSString *contents; Objective-C #import "Card.h" @interface Card() @implementation Card Card.m In ios, we don t access instance variables directly. Instead, we use an @property which declares two methods: a setter and a getter. It is with those two methods that the @property s instance variable is accessed (both publicly and privately). This particular @property is a pointer. Specifically, a pointer to an object whose class is (or inherits from) NSString. ALL objects live in the heap (i.e. are pointed to) in Objective-C! Thus you would never have a property of type NSString (rather, NSString * ). Because this @property is in this class s header file, it is public. Its setter and getter can be called from outside this class s @implementation block.

Card.h #import <Foundation/Foundation.h> @interface Card : NSObject @property (strong) NSString *contents; Objective-C #import "Card.h" @interface Card() @implementation Card Card.m strong means: keep the object that this property points to in memory until I set this property to nil (zero) (and it will stay in memory until everyone who has a strong pointer to it sets their property to nil too) weak would mean: if no one else has a strong pointer to this object, then you can throw it out of memory and set this property to nil (this can happen at any time)

Card.h #import <Foundation/Foundation.h> @interface Card : NSObject Objective-C #import "Card.h" @interface Card() @implementation Card Card.m @property (strong, nonatomic) NSString *contents; nonatomic means: access to this property is not thread-safe. We will always specify this for object pointers in this course. If you do not, then the compiler will generate locking code that will complicate your code elsewhere.

Card.h #import <Foundation/Foundation.h> @interface Card : NSObject Objective-C #import "Card.h" @interface Card() @implementation Card Card.m This @synthesize is the line of code that actually creates the backing instance variable that is set and gotten. Notice that by default the backing variable s name is the same as the property s name but with an underbar in front. @property (strong, nonatomic ) NSString *contents; @synthesize contents = _contents; This is the @property implementation that the compiler generates automatically for you (behind the scenes). You are welcome to write the setter or getter yourself, but this would only be necessary if you needed to do something in addition to simply setting or getting the value of the property. - (NSString *)contents { return _contents; } - (void)setcontents:(nsstring *)contents { _contents = contents; }

Card.h #import <Foundation/Foundation.h> @interface Card : NSObject Objective-C #import "Card.h" @interface Card() @implementation Card Card.m @property (strong, nonatomic) NSString *contents; Because the compiler takes care of everything you need to implement a property, it s usually only one line of code (the @property declaration) to add one to your class.

Card.h #import <Foundation/Foundation.h> Objective-C #import "Card.h" Card.m Notice no strong or weak here. Primitive types are not stored in the heap, so there s no need to specify how the storage for them in the heap is treated. @interface Card : NSObject @property (strong, nonatomic) NSString *contents; @property (nonatomic) BOOL chosen; @property (nonatomic) BOOL matched; Properties can be any C type. That includes int, float, etc., even C structs. C does not define a boolean type. This BOOL is an Objective-C typedef. It s values are YES or NO. @interface Card() @implementation Card Let s look at some more properties. These are not pointers. They are simple BOOLs.

Card.h #import <Foundation/Foundation.h> @interface Card : NSObject Objective-C #import "Card.h" @interface Card() @implementation Card Card.m @property (strong, nonatomic) NSString *contents; @property (nonatomic) BOOL chosen; @property (nonatomic) BOOL matched; @synthesize chosen = _chosen; @synthesize matched = _matched; - (BOOL) chosen { return _chosen; } - (void)setchosen:(bool)chosen { _chosen = chosen; } - (BOOL) matched { return _matched; } - (void)setmatched:(bool)matched { _matched = matched; } Here s what the compiler is doing behind the scenes for these two properties.

Card.h #import <Foundation/Foundation.h> Objective-C #import "Card.h" Card.m @interface Card : NSObject @interface Card() It is actually possible to change the name of the getter that is generated. The only time you ll ever see that done in this class (or anywhere probably) is boolean getters. @implementation Card @property (strong, nonatomic) NSString *contents; @property (nonatomic, getter=ischosen) BOOL chosen; @property (nonatomic, getter=ismatched) BOOL matched; This is done simply to make the code read a little bit nicer. You ll see this in action later. @synthesize chosen = _chosen; @synthesize matched = _matched; - (BOOL) ischosen { return _chosen; } - (void)setchosen:(bool)chosen { _chosen = chosen; } - (BOOL) ismatched { return _matched; } - (void)setmatched:(bool)matched { _matched = matched; } Note change in getter method. Note change in getter method.

Card.h #import <Foundation/Foundation.h> @interface Card : NSObject Objective-C #import "Card.h" @interface Card() @implementation Card Card.m @property (strong, nonatomic) NSString *contents; @property (nonatomic, getter=ischosen) BOOL chosen; @property (nonatomic, getter=ismatched) BOOL matched; Remember, unless you need to do something besides setting or getting when a property is being set or gotten, the implementation side of this will all happen automatically for you.

Card.h #import <Foundation/Foundation.h> @interface Card : NSObject Objective-C #import "Card.h" @interface Card() @implementation Card Card.m @property (strong, nonatomic) NSString *contents; Enough properties for now. Let s take a look at defining methods. @property (nonatomic, getter=ischosen) BOOL chosen; @property (nonatomic, getter=ismatched) BOOL matched; - (int)match:(card *)card; Here s the declaration of a public method called match: which takes one argument (a pointer to a Card) and returns an integer. What makes this method public? Because we ve declared it in the header file.

Card.h #import <Foundation/Foundation.h> @interface Card : NSObject Objective-C #import "Card.h" @interface Card() @implementation Card Card.m @property (strong, nonatomic) NSString *contents; @property (nonatomic, getter=ischosen) BOOL chosen; @property (nonatomic, getter=ismatched) BOOL matched; - (int)match:(card *)card; - (int)match:(card *)card { int score = 0; Here s the declaration of a public method called match: which takes one argument (a pointer to a Card) and returns an integer. match: is going to return a score which says how good a match the passed card is to the Card that is receiving this message. 0 means no match, higher numbers mean a better match. } return score;

Card.h #import <Foundation/Foundation.h> Objective-C #import "Card.h" @interface Card() Card.m @interface Card : NSObject @property (strong, nonatomic) NSString *contents; @property (nonatomic, getter=ischosen) BOOL chosen; @property (nonatomic, getter=ismatched) BOOL matched; @implementation Card There s a lot going on here! For the first time, we are seeing the calling side of properties (and methods). - (int)match:(card *)card; - (int)match:(card *)card { int score = 0; if ([card.contents isequaltostring:self.contents]) { score = 1; } } return score; For this example, we ll return 1 if the passed card has the same contents as we do or 0 otherwise (you could imagine more complex scoring).

Card.h #import <Foundation/Foundation.h> Objective-C #import "Card.h" @interface Card() Card.m @interface Card : NSObject @property (strong, nonatomic) NSString *contents; @property (nonatomic, getter=ischosen) BOOL chosen; @property (nonatomic, getter=ismatched) BOOL matched; @implementation Card Notice that we are calling the getter for the contents @property (both on our self and on the passed card). This calling syntax is called dot notation. It s only for setters and getters. - (int)match:(card *)card; - (int)match:(card *)card { int score = 0; if ([card.contents isequaltostring:self.contents]) { score = 1; } } return score;

Card.h #import <Foundation/Foundation.h> Objective-C #import "Card.h" @interface Card() Card.m @interface Card : NSObject Recall that the contents property is an NSString. @implementation Card @property (strong, nonatomic) NSString *contents; @property (nonatomic, getter=ischosen) BOOL chosen; @property (nonatomic, getter=ismatched) BOOL matched; - (int)match:(card *)card; - (int)match:(card *)card { int score = 0; isequaltostring: is an NSString method which takes another NSString as an argument and returns a BOOL (YES if the 2 strings are the same). if ([card.contents isequaltostring:self.contents]) { score = 1; } } Also, we see the square bracket notation we use to send a message to an object. In this case, the message isequaltostring: is being sent to the NSString returned by the contents getter. return score;

Card.h #import <Foundation/Foundation.h> @interface Card : NSObject Objective-C #import "Card.h" @interface Card() @implementation Card Card.m @property (strong, nonatomic) NSString *contents; @property (nonatomic, getter=ischosen) BOOL chosen; @property (nonatomic, getter=ismatched) BOOL matched; - (int)match:(nsarray *)othercards; - (int)match:( NSArray *)othercards { int score = 0; We could make match: even more powerful by allowing it to match against multiple cards by passing an array of cards using the NSArray class in Foundation. } if ([card.contents isequaltostring:self.contents]) { score = 1; } return score;

Card.h #import <Foundation/Foundation.h> Objective-C #import "Card.h" @interface Card() Card.m @interface Card : NSObject @property (strong, nonatomic) NSString *contents; @property (nonatomic, getter=ischosen) BOOL chosen; @property (nonatomic, getter=ismatched) BOOL matched; @implementation Card We ll implement a very simple match scoring system here which is to score 1 point if ANY of the passed othercards contents match the receiving Card s contents. (You could imagine giving more points if multiple cards match.) - (int)match:(nsarray *)othercards; - (int)match:( NSArray *)othercards { int score = 0; } for (Card *card in othercards) { if ([card.contents isequaltostring:self.contents]) { score = 1; } } return score; Note the for-in looping syntax here. This is called fast enumeration. It works on arrays, dictionaries, etc.

Coming Up Next Lecture More of our Card Game Model Xcode 5 Demonstration (start building our Card Game application) Next Week Finish off our Card Game application Objective-C in more detail Foundation (array, dictionary, etc.) And much much more!