iphone SDK Enrolled students will be invited to developer program Login to Program Portal Request a Certificate Download and install the SDK



Similar documents
iphone Objective-C Exercises

Objective-C and Cocoa User Guide and Reference Manual. Version 5.0

Object Oriented Programming and the Objective-C Programming Language 1.0. (Retired Document)

Introduction to Objective-C. Kevin Cathey

INTRODUCTION TO OBJECTIVE-C CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 12 09/29/2011

Praktikum Entwicklung von Mediensystemen mit

Objective-C Tutorial

Objective C and iphone App

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Chapter 1 Fundamentals of Java Programming

Compiling Object Oriented Languages. What is an Object-Oriented Programming Language? Implementation: Dynamic Binding

Creating a Custom Class in Xcode

Introducing Variance into the Java Programming Language DRAFT

Assignment I Walkthrough

UNIVERSITY OF CALIFORNIA BERKELEY Engineering 7 Department of Civil and Environmental Engineering. Object Oriented Programming and Classes in MATLAB 1

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

From C++ to Objective-C


Part I. Multiple Choice Questions (2 points each):

History OOP languages Year Language 1967 Simula Smalltalk

Implementation Aspects of OO-Languages

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

Agenda. What is and Why Polymorphism? Examples of Polymorphism in Java programs 3 forms of Polymorphism

Objective-C for Experienced Programmers

Common Lisp ObjectiveC Interface

CS193j, Stanford Handout #10 OOP 3

SE 360 Advances in Software Development Object Oriented Development in Java. Polymorphism. Dr. Senem Kumova Metin

C Programming Review & Productivity Tools

6.088 Intro to C/C++ Day 4: Object-oriented programming in C++ Eunsuk Kang and Jean Yang

This documentation is made available before final release and is subject to change without notice and comes with no warranty express or implied.

Key-Value Coding Programming Guide

Fundamentals of Java Programming

MA-WA1920: Enterprise iphone and ipad Programming

Objective-C Internals

Object-Oriented Programming

ios Development Tutorial Nikhil Yadav CSE 40816/60816: Pervasive Health 09/09/2011

Using PyObjC for Developing Cocoa Applications with Python

Introduction to Object-Oriented Programming

AC : OBJECTIVE-C VERSUS JAVA FOR SMART PHONE AP- PLICATIONS

UML FOR OBJECTIVE-C. Excel Software

ios App Development for Everyone

The C Programming Language course syllabus associate level

ios Dev Fest Research Network Operations Center Thursday, February 7, 13

Inheritance in Programming Languages

Java programming for C/C++ developers

The Smalltalk Programming Language. Beatrice Åkerblom

Object Oriented Software Design - I

Brent A. Perdue. July 15, 2009

CORBA Programming with TAOX11. The C++11 CORBA Implementation

Java Programming Language

core. Volume I - Fundamentals Seventh Edition Sun Microsystems Press A Prentice Hall Title ULB Darmstadt

Bonus ChapteR. Objective-C

Specialized Android APP Development Program with Java (SAADPJ) Duration 2 months

Habanero Extreme Scale Software Research Project

ios Dev Crib Sheet In the Shadow of C

On the (un)suitability of Java to be the first programming language

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

Chapter 13 - Inheritance

El Dorado Union High School District Educational Services

The Interface Concept

3 Multimedia Programming with C++ and Multimedia Frameworks

Concepts and terminology in the Simula Programming Language

Programming Language Rankings. Lecture 15: Type Inference, polymorphism & Type Classes. Top Combined. Tiobe Index. CSC 131! Fall, 2014!

Compile-time type versus run-time type. Consider the parameter to this function:

1.1.3 Syntax The syntax for creating a derived class is very simple. (You will wish everything else about it were so simple though.

Server-Side Scripting and Web Development. By Susan L. Miertschin

Using the Caché Objective-C Binding

Programming Language Concepts: Lecture Notes

Glossary of Object Oriented Terms

An Incomplete C++ Primer. University of Wyoming MA 5310

CS 111 Classes I 1. Software Organization View to this point:

Java (12 Weeks) Introduction to Java Programming Language

Java Interview Questions and Answers

Object-Oriented Programming Basics With Java

Polymorphism. Problems with switch statement. Solution - use virtual functions (polymorphism) Polymorphism

1 bool operator==(complex a, Complex b) { 2 return a.real()==b.real() 3 && a.imag()==b.imag(); 4 } 1 bool Complex::operator==(Complex b) {

Adapter, Bridge, and Façade

AP Computer Science Java Subset

Networks and Protocols Course: International University Bremen Date: Dr. Jürgen Schönwälder Deadline:

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer

Polymorphism. Why use polymorphism Upcast revisited (and downcast) Static and dynamic type Dynamic binding. Polymorphism.

Inside Mac OS X. The Objective-C Programming Language

CS506 Web Design and Development Solved Online Quiz No. 01

What is COM/DCOM. Distributed Object Systems 4 COM/DCOM. COM vs Corba 1. COM vs. Corba 2. Multiple inheritance vs multiple interfaces

COMPARISON OF OBJECT-ORIENTED AND PROCEDURE-BASED COMPUTER LANGUAGES: CASE STUDY OF C++ PROGRAMMING

If you wanted multiple screens, there was no way for data to be accumulated or stored

An Overview of Java. overview-1

Computing Concepts with Java Essentials

Realizing Enterprise Integration Patterns in WebSphere

ios Audio Programming Guide

Java 6 'th. Concepts INTERNATIONAL STUDENT VERSION. edition

Pentesting ios Apps Runtime Analysis and Manipulation. Andreas Kurtz

Star Micronics Cloud Services ios SDK User's Manual

1. What are Data Structures? Introduction to Data Structures. 2. What will we Study? CITS2200 Data Structures and Algorithms

Chapter 1. Introduction to ios Development. Objectives: Touch on the history of ios and the devices that support this operating system.

Description of Class Mutation Mutation Operators for Java

Using Inheritance and Polymorphism

The Cool Reference Manual

java Features Version April 19, 2013 by Thorsten Kracht

Transcription:

Objective-C Basics

iphone SDK Enrolled students will be invited to developer program Login to Program Portal Request a Certificate Download and install the SDK

The First Program in Objective-C #import <Foundation/Foundation.h> int main (int argc, const char * argv[]) { NSLog(@"Hello, World!"); } return 0;

OOP Vocabulary Class: defines the grouping of data and code, the type of an object Instance: a specific allocation of a class Method: a function that an object knows how to perform Instance Variable (or ivar ): a specific piece of data belonging to an object

OOP Vocabulary Encapsulation keep implementation private and separate from interface Polymorphism different objects, same interface Inheritance hierarchical organization, share code, customize or extend behaviors

Inheritance Hierarchical relation between classes Subclass inherit behavior and data from superclass Subclasses can use, augment or replace superclass methods

More OOP Info? Tons of books and articles on OOP Most Java or C++ book have OOP introductions Objective-C 2.0 Programming Language http://developer.apple.com/documentation/ Cocoa/Conceptual/ObjectiveC

Objective-C Strict superset of C Mix C with ObjC Or even C++ with ObjC (usually referred to as ObjC+ +) A very simple language, but some new syntax Single inheritance, classes inherit from one and only one superclass Protocols define behavior that cross classes Dynamic runtime

Classes and Instances In Objective-C, classes and instances are both objects Class is the blueprint to create instances

Classes and Objects Classes declare state and behavior State (data) is maintained using instance variables Behavior is implemented using methods Instance variables typically hidden Accessible only using getter/setter methods

Object

Behavior

Message

State

Other Objects As State

OOP From ObjC Perspective Everybody has their own spin on OOP Apple is no different For the spin on OOP from an ObjC perspective: Read the Object-Oriented Programming with Objective-C document http://developer.apple.com/iphone/library/ documentation/cocoa/conceptual/oop_objc

Class and Instance Methods Instances respond to instance methods (id) init; (float) height; (void) walk; Classes respond to class methods + (id) alloc; + (id) person; + (Person *) sharedperson;

Message syntax [receiver message] [receiver message:argument] [receiver message:arg1 andarg:arg2]

Message examples Person *voter; //assume this exists [voter castballot]; int theage = [voter age]; [voter setage:21]; if ([voter canlegallyvote]) { // do something voter-y } [voter registerforstate:@ OH" party:@"independant"]; NSString *name = [[voter spouse] name];

Terminology Message expression [receiver method: argument] Message [receiver method: argument] Selector [receiver method: argument] Method The code selected by a message

The First OO-Program in Objective-C Shape-Procedure: Suppose a program draws a bunch of geometric shapes on the screen: circle, square, egg-shaped (Color and Boundary) Let us first take a look of the procedure-c program! NSString *colorname (ShapeColor color) { switch (color) { case kredcolor: return @"red"; break; } // colorname return @"no clue"; void drawcircle (ShapeRect bounds, ShapeColor fillcolor) { NSLog (@"drawing a circle at (%d %d %d %d) in %@", bounds.x, bounds.y, bounds.width, bounds.height, colorname (fillcolor)); } // drawcircle

Class Definition @interface Circle: NSObject { ShapeColor fillcolor; ShapeRect bounds; } - (void) setfillcolor: (ShapeColor) fillcolor; - (void) setbounds: (ShapeRect) bound; - (void) draw; @end // Circle;

Class Implementation @implementation Circle - (void) setfillcolor: (ShapeColor) c { fillcolor = c; } //setfillcolor - (void) setbounds: (ShapeRect) b { bounds=b; } //setbound - (void) draw { NSLog (@"drawing a circle at (%d %d %d %d) in %@", bounds.x, bounds.y, bounds.width, bounds.height, colorname (fillcolor)); } //draw @end

Message Examples int main (int argc, const char * argv[]) { id shapes[3]; void drawshapes (id shapes[], int count) { int i; } // drawshapes for (i=0; i< count; i++) { [shapes[i] draw]; } ShapeRect rect0 = {0, 0, 10, 30}; shapes[0] = [Circle new]; [shapes[0] setbounds: rect0]; [shapes[0] setfillcolor:kredcolor]; ShapeRect rect1 = {30, 40, 50, 60}; shapes[1] = [Rectangle new]; [shapes[1] setbounds: rect1]; [shapes[1] setfillcolor: kgreencolor]; ShapeRect rect2 = {15, 19, 37, 29}; shapes[2] = [OblateSphereoid new]; [shapes[2] setbounds: rect2]; [shapes[2] setfillcolor: kbluecolor]; drawshapes (shapes, 3); } return 0;

Inheritance (Common Class) @interface Shape: NSObject { ShapeColor fillcolor; ShapeRect bounds; } - (void) setfillcolor: (ShapeColor) fillcolor; - (void) setbounds: (ShapeRect) bound; - (void) draw; @end // Shape; @implementation Shape - (void) setfillcolor: (ShapeColor) c { fillcolor = c; } //setfillcolor - (void) setbounds: (ShapeRect) b { bounds=b; } //setbound - (void) draw { } //draw @end

@end Inheritance (Common Class) @implementation Circle @interface Circle: Shape @end // Circle; @interface Rectangle: Shape @end // Rectangle; @interface OblateSphereoid: Shape @end // OblateSphereoid; - (void) setfillcolor: (ShapeColor) c { if (c==kredcolor) { c=kgreencolor; } [super setfillcolor:c]; } // setfillcolor - (void) draw { NSLog (@"drawing a circle at (%d %d %d %d) in %@", bounds.x, bounds.y, bounds.width, bounds.height, colorname (fillcolor)); } //draw

Dynamic and static typing Dynamically-typed object id anobject Just id Not id * (unless you really, really mean it...) Statically-typed object Person *anobject Objective-C provides compile-time, not runtime, type checking Objective-C always uses dynamic binding

The null object pointer Test for nil explicitly if (person == nil) return; Or implicitly if (!person) return; Can use in assignments and as arguments if expected person = nil; [button settarget: nil]; Sending a message to nil? person = nil; [person castballot];

Bool areintsdifferent (int thing1, int thing2) { return (thing1-thing2); } if (areintsdifferent(23,5)==yes) { } if (areintsdifferent(23,5)){ }

BOOL typedef When ObjC was developed, C had no boolean type (C99 introduced one) ObjC uses a typedef to define BOOL as a type BOOL flag = NO; Macros included for initialization and comparison: YES and NO if (flag == YES) if (flag) if (!flag) if (flag!= YES) flag = YES; flag = 1;