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

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

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

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

Announcement. Final Project Proposal Presentations and Updates

Objective-C Part 2 (ECS189H) Ken Joy Serban Porumbescu

CS193E Lecture #3 Categories and Protocols Cocoa Memory Management

COMP327 Mobile Computing Session: Tutorial Objective-C and the Foundation Framework

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

CS193p Spring 2010 Thursday, April 29, 2010

Object-Oriented Programming with Objective-C. Lecture 2

Objective-C. Stanford CS193p Fall 2013

ITP 342 Advanced Mobile App Dev. Memory

Mobile Application Development

IPHONE DEVELOPMENT. Getting Started with the iphone SDK

ios: Objective-C Primer

Objective-C and COCOA Applications

CS 47. Beginning iphone Application Development

Advanced Object- C Features

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

Mobile Application Development

IPHONE. Development Jump Start. phil nash levelofindirection.com

Announcements. Today s Topics

Mobile Application Development

Tools & Languages: Objective-C

ios Development Lecture 1 Introduction to Objective-C Ing. Simone Cirani

Collections & Memory Management. Lecture 2

Tools & Languages: Objective-C

Objective-C and Cocoa User Guide and Reference Manual

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?

Mobile Application Programming. Objective-C Classes

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

About MSDOSX. Lecture 0

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

Principles of Programming Languages. Objective-C. Joris Kluivers

Topics in Mobile Computing

Introductory ios Development

ITP 342 Mobile App Dev. Fundamentals

Object Oriented Programming in C++

MacOS X Cocoa Programming for Vision Scientist --- #1

Cocoa > Objective-C Language

CS193P - Lecture 2. iphone Application Development. Objective-C Foundation Framework

CS193P - Lecture 2. iphone Application Development. Objective-C Foundation Framework

Cocoa Programming. David Chisnall. March 18,

OBJECTIVE-C BEST PRACTICES IN A TEAM ENVIRONMENT

COCOA WORKSHOP PART 1. Andreas Monitzer

Objective-C Runtime. Cocoa s Jewel in the Crown. NSConference Nicolas

Review (Basic Objective-C)

Objects. say something to express one's disapproval of or disagreement with something.

ITP 342 Mobile App Dev. Fundamentals

Working Effectively with Objective-C on iphone OS. Blaine Garst Wizard of Runtimes

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

MechEng SE3 Lecture 7 Domain Modelling

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

Assignment II: Foundation Calculator

src7-malan/c/array/array/main.c // main.c // Array // David J. Malan // Harvard University // // Demonstrates arrays. 11.

Praktikum Entwicklung von Mediensystemen mit ios

By Ryan Hodson. Foreword by Daniel Jebaraj

iphone Software Development

epicurious Anatomy of an ios app Robert Tolar Haining June 25, 2010 ConvergeSE

Objective-C Primer. iphone Programmer s Association. Lorenzo Swank September 10, 2008

Monday, 1 November The ios System

Getting Help. iphone Application Programming Lecture 3: Foundation Classes. Data Structures in Objective C. Online Documentation.

iphone Application Programming Lecture 3: Foundation Classes

1.The basics 5 Comments 5 Variables and basic types 6 Operators 8 Object variables 10 A note on prefixes and namespaces 11

CS193p Spring 2010 Wednesday, March 31, 2010

Lecture 13: more class, C++ memory management

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Mobile Application Programming. Memory Management

The MVC Design Pattern

ITP 342 Mobile App Dev. Data Types

CISC 1600 Lecture 3.1 Introduction to Processing

Memory Management: The Details

CS445 Week 9: Lecture

Data Abstraction. Hwansoo Han

Data Structures (list, dictionary, tuples, sets, strings)

Porting Objective-C to Swift. Richard Ekle

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

iphone Programming Patrick H. Madden SUNY Binghamton Computer Science Department

More C++ : Vectors, Classes, Inheritance, Templates

Mobile App Development. ios Platform

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

2. The object-oriented paradigm!

CPS 506 Comparative Programming Languages. Programming Language

Programs as Models. Procedural Paradigm. Class Methods. CS256 Computer Science I Kevin Sahr, PhD. Lecture 11: Objects

iphone Application Programming Lecture 3: Swift Part 2

Core Animation. Building Animated UI s. Bill Dudney. Gala Factory Software LLC. Bill Dudney Core Animation: Building Animated UI s Slide 1

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Bindings Example Exercise James Dempsey - WWDC Pre-Show Cocoa Workshop

Thread Sanitizer and Static Analysis

Polymorphism. Object Orientated Programming in Java. Benjamin Kenwright

Manual Allocation. CS 1622: Garbage Collection. Example 1. Memory Leaks. Example 3. Example 2 11/26/2012. Jonathan Misurda

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

Cunning Plan. One-Slide Summary. Functional Programming. Functional Programming. Introduction to COOL #1. Classroom Object-Oriented Language

NSObject. - (NSString *)description Provides us with a string description of the object

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

COSC252: Programming Languages: Abstraction and OOP. Jeremy Bolton, PhD Asst Teaching Professor. Copyright 2015 Pearson. All rights reserved.

Mac OS X and ios operating systems. Lecture 2 Objective-C. Tomasz Idzi

C++ (Non for C Programmer) (BT307) 40 Hours

Motivation was to facilitate development of systems software, especially OS development.

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

Transcription:

Review iphone Application Programming Lecture 2: Objective-C, Cocoa Device restrictions Gero Herkenrath Media Computing Group RWTH Aachen University Winter Semester 2013/2014 http://hci.rwth-aachen.de/iphone Interaction paradigm changes 2 History Roots in the early 1980 s Objective-C Strongly influenced by Smalltalk Mainly used in NeXTSTEP, OS X, iphone OS 3 4

Characteristics Syntax differences from C Strict superset of C Can be mixed with C and C++ Single inheritance Dynamic runtime Loosely typed (if you want it) Memory management Automatic or manual reference counting New types Anonymous object (id) Class Selector Class definition Object messaging Properties Enumeration 5 6 New Types Object Identifier: id // represents an objective-c class Class aclass // reference to an objective-c method SEL somemethod // reference to an instance of a specific objective-c class Person *aperson // reference to an instance of any objective-c class id aperson // boolean (YES or NO) BOOL isalive // id is defined as a pointer to an object typedef struct objc_object Class isa; *id; // The Class type is defined as a pointer typedef struct objc_class *Class; 7 8

Dynamic Typing Object Messaging // message without argument [receiver message] id is completely unrestrictive The type of an object is only determined at runtime Allows for introspection // message with single argument [receiver message:argument] // message with multiple arguments [receiver message:arg1 argument2:arg2] // receiving a return value int status = [receiver message]; // message with a variable number of arguments [receiver makegroup:group, memberone, membertwo, memberthree]; 9 10 Object Messaging Terminology Object Messaging Message expression [receiver method:argument] Message [receiver method:argument] Selector [receiver method:argument] Method The code selected by a message // message without argument [rectangle draw] // message with single argument [rectangle setcolor:blue] // message with multiple arguments [rectangle setstrokecolor:blue andthickness:2.0] // receiving a return value double length = [rectangle circumference]; // message with a variable number of arguments [NSArray arraywithobjects:one, two, three, nil]; 11 12

Exercise Identity vs. Equality Java Rectangle rect = Rectangle(someWidth, someheight); println(rect.getheight()); boolean inside = rect.contains(x,y); Objective-C Rectangle *rect = [[Rectangle alloc] initwithwidth:somewidth!!!!!!!!!!!!!!!! andheight:someheight]; NSLog(@"%f",[rect height]); BOOL inside = [rect containspointwithx:x andy:y] // test for object identity: BOOL identical = @"Peter" == @"Peter"; //YES!! identical = @"Peter" == @"Peter "; //NO // test for string equality: will return YES NSString *aname = @"Peter"; NSMutableString *anothername =! [NSMutableString stringwithstring:@"p"]; [anothername appendstring:@"eter"]; BOOL same = [aname isequaltostring:anothername];! //YES BOOL equal = (aname == anothername);!!!!!! //NO // test for existence BOOL exists = (object!= nil); 13 14 Selector Class Inheritance id object; // create a selector for action: SEL anaction = @selector(action:); // check if the selector can be performed on object if([object respondstoselector:anaction])!! // perform the selector on object with a parameter! [object performselector:anaction withobject:self]; Class definitions are additive Inherit variables and methods from a class Modify or add functionality NSObject Graphic Shape Text Image Line Rectangle Circle Square 15 16

NSObject Class Definition NSObject is root class Similar to class Object in Java Provides functionality for Memory management (allocation, release) Object equality Introspection Header File contains public class declaration Implementation File contains method implementation Abstract class To use a class, import the header file The implementation file must also import its own header 17 18 Class Definition: Header Class Definition: Implementation #import <Foundation/Foundation.h>! NSString *name;! int age; // accessor for name - (NSString *)name; - (void)setname:(nsstring *)newname; // accessor for age - (int)age; - (void)setage:(int)newage; // actions -(BOOL)isAllowedToVote; -(void)castballot; #import "Person.h" - (int)age! return age; - (void)setage:(int)value! age = value; // and other methods 19 20

Using a Class Class Instantiation To create an instance of a class Dynamically allocate memory Initialize that memory with appropriate values id anobject = [[SomeClass alloc] init]; - (id) init! self = [super init];! if (self)!! // initializations!! return self; - (Person *)createpersonwithname:(nsstring *)aname! // create a new instance of the class Person! Person *aperson = [[Person alloc] init];!! // use the accessors to assign property values! [aperson setname:aname];! [aperson setage:21];!! // return the instance! return aperson;! 21 22 Class Inheritance @interface Student : Person! NSString *major; @implementation Student // Constructor for a student - (id) init! self = [super init];! if (self!= nil)!! // set attributes!! [self setmajor:@"undefined"];!! return self; Demo 23 24

Protocols Review // MyServiceDelegate.h (or MyService.h) @protocol MyServiceDelegate // required protocol method - (void)myservice:(myservice *)myservice didupdatewithresult: (id)object; // optional methods @optional // MyController.h @interface MyController : NSObject <MyServiceDelegate> // For Objects: id<myservicedelegate> anobject; History Characteristics Naming conventions How would you name a function that inserts a string into an existing string at a certain index? 25 26 Categories Class Extensions // NSString+Moo.h @interface NSString (Moo) // define a new method for NSString - (NSString *)stringwithmoo; // NSString+Moo.m @implementation NSString (Moo) // implement the new method for NSString - (NSString *)stringwithmoo! return [self stringbyappendingstring:@"moo"]; // extend NSString with the category #import "NSString+Moo.h" // log FooMoo to console NSLog([@"Foo" stringwithmoo]);! // define instance variables! NSNumber *age;! BOOL! pictureisuploaded; - (void)startpictureupload; 27 28

Class Extensions Class Extensions! // define instance variables! NSNumber *age; @interface Person ()! BOOL! pictureisuploaded; - (void)startpictureupload;! // define instance variables! NSNumber *age; @interface Person ()! BOOL! pictureisuploaded; 29 30 Properties Demo! int age; // accessor for age - (int)age; - (void)setage:(int)newage; - (int)age! return age; - (void)setage:(int)value! age = value; 31 32

Properties Properties! int age; @synthesize age;! int _age; @synthesize age; // age property @property(assign) int age; // age property @property(assign) int age; 33 34 Properties Using Properties // age property @property(assign) int age; // The statements below result in the exact same code // access the value of a property age = person.age; age = [person age]; // change the value of a property person.age = 12; [person setage:12]; document.author.name = @ Peter ; [[document author] setname:@ Peter ]; 35 36

Advanced Properties (Fast) Enumeration // Imagine a class Rectangle // A property does not necessarily have to relate to an ivar @property (readonly) float circumference; // The value is only calculated when required - (float)circumference! return 2*(width+height); // we have an array of Person objects NSArray *people = ; // declare person variable outside the for loop Person *person; // fast enumeration of the people array for(person in people)! // do something with all persons in the array! [person castballot]; 37 38 Memory Management Memory Management Garbage collection Java C Core Foundation Malloc/free Cocoa / UIKit Retain/Release ARC 39 40

Object Lifecycle Basic Rules Objective-C uses reference counting [object retain] increases retain counter [object release] decreases retain counter object is deallocated if the retain counter reaches 0 Object is initialized with retain count of 1 [NSObject alloc] [NSObject copy] You own an object you create You take ownership of an object by calling retain You must relinquish ownership of unused objects you own (by calling release or autorelease) 41 42 The Retain Count Clean up NSNumber *age = [[NSNumber alloc] initwithint:42];!!!!!! //retain count = 1 [age retain];! //retain count = 2 [age release];!//retain count = 1 [age release];!//retain count = 0 -> Object deallocated @interface Person : NSObject @property(retain) NSNumber *age; - (id)init! NSNumber *someage = [[NSNumber alloc] initwithint:42];! self.age = someage; [someage release] - (void)dealloc! // With synthesized setters, you set the object! // to nil to release it! // If delegate would be just a simple ivar,! // we would call [_delegate release];! self.age = nil; 43 44

Retain/release for Instance Variables @interface Person! NSString *name; - (void)setname:(nsstring *)aname! if(name == aname) return; // prevent unnecessary assignments!! // always retain before releasing! [aname retain];! [name release];!! name = aname; // forget the old name and assign the new name // cleanup (called when this object is released) - (void)dealloc! [name release];! [super dealloc]; (to be continued next week) 45 46