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

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

INTRODUCTION TO IOS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 13 02/22/2011

iphone Objective-C Exercises

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

Introduction to Objective-C. Kevin Cathey

ios App Development for Everyone

Assignment I Walkthrough

Creating a Custom Class in Xcode

ios Dev Crib Sheet In the Shadow of C

Objective C and iphone App

ios App Development for Everyone

An Introduction to Modern Software Development Tools Creating A Simple GUI-Based Tool Appleʼs XCode Version 3.2.6

Praktikum Entwicklung von Mediensystemen mit

MA-WA1920: Enterprise iphone and ipad Programming

Introduction to Eclipse

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

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

ios App Development for Everyone

Xcode Project Management Guide. (Legacy)

C++ INTERVIEW QUESTIONS

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

Installing Java 5.0 and Eclipse on Mac OS X

Xcode Application note

Building Mobile Applications Creating ios applications with jquery Mobile, PhoneGap, and Drupal 7

INTRODUCTION TO ANDROID CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 11 02/15/2011

How to test and debug an ASP.NET application

Developing Applications for ios

Using Karel with Eclipse

Install Java Development Kit (JDK) 1.8

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

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

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

Hands-on Practice. Hands-on Practice. Learning Topics

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

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc.

Java Interview Questions and Answers

University of Twente. A simulation of the Java Virtual Machine using graph grammars

Using the Caché Objective-C Binding

Programming with the Dev C++ IDE

Installing Java. Table of contents

Chapter 1: Getting Started

Part 1 Foundations of object orientation

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

Introduction to Programming with Xojo

Fundamentals of Java Programming

MEAP Edition Manning Early Access Program Hello! ios Development version 14

The C Programming Language course syllabus associate level

UML for C# Modeling Basics

1 Using the JavaConverter

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

Using PyObjC for Developing Cocoa Applications with Python

Lecture 1 Introduction to Android

Chapter 1 Fundamentals of Java Programming

Application Note C++ Debugging

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Your First ios Application

Project Builder for Java. (Legacy)

Getting Started with the Internet Communications Engine

How to Borrow Adobe EPUB Ebooks From Your iphone and ipod Touch

Chief Architect X6. Download & Installation Instructions. Chief Architect, Inc N. Mineral Dr. Coeur d Alene, Idaho

Glossary of Object Oriented Terms

Getting Started with Command Prompts

Contents. About Testing with Xcode 4. Quick Start 7. Testing Basics 23. Writing Test Classes and Methods 28. At a Glance 5 Prerequisites 6 See Also 6

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Ohio University Computer Services Center August, 2002 Crystal Reports Introduction Quick Reference Guide

A product of Byte Works, Inc. Credits Programming Mike Westerfield. Art Karen Bennett. Documentation Mike Westerfield

3. Locate the different selections of Styles from the Home Tab, Styles Group

Business Portal for Microsoft Dynamics GP. Requisition Management User s Guide Release 10.0

How to develop your own app

Xcode User Default Reference. (Legacy)

Knappsack ios Build and Deployment Guide

Vim, Emacs, and JUnit Testing. Audience: Students in CS 331 Written by: Kathleen Lockhart, CS Tutor

Cocoa Fundamentals Guide. (Retired Document)

Steps for Entering an OnBase Purchase Requisition (PR)

After you complete the survey, compare what you saw on the survey to the actual questions listed below:

Tutorial: Getting Started

Android Studio Application Development

CMPT 183 Foundations of Computer Science I

MAIL MERGE MADE EASY A STEP-BY-STEP GUIDE FOR LABELS OR MERGES

Many home and small office networks exist for no

How to Concatenate Cells in Microsoft Access

How to get MOSS 2007 dev. environment set up in Vista with sample project.

Google Drive: Access and organize your files

Website Development Komodo Editor and HTML Intro

Getting Started with Amazon EC2 Management in Eclipse

MONITORING PERFORMANCE IN WINDOWS 7

Lecture 5: Java Fundamentals III

Titanium Mobile: How-To

Sources: On the Web: Slides will be available on:

3 What s New in Excel 2007

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Salesforce Mobile Push Notifications Implementation Guide

Java Programming. Binnur Kurt Istanbul Technical University Computer Engineering Department. Java Programming. Version 0.0.

AutoDWG DWGSee DWG Viewer. DWGSee User Guide

Quick Guide. Passports in Microsoft PowerPoint. Getting Started with PowerPoint. Locating the PowerPoint Folder (PC) Locating PowerPoint (Mac)

How to install and use the File Sharing Outlook Plugin

DWGSee Professional User Guide

Getting Started with Excel Table of Contents

Deposit Direct. Getting Started Guide

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Transcription:

INTRODUCTION TO OBJECTIVE-C CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 12 02/17/2011 1

Goals of the Lecture Present an introduction to Objective-C 2.0 Coverage of the language will be INCOMPLETE We ll see the basics there is a lot more to learn 2

History (I) Brad Cox created Objective-C in the early 1980s It was his attempt to add object-oriented programming concepts to the C programming language NeXT Computer licensed the language in 1988; it was used to develop the NeXTSTEP operating system, programming libraries and applications for NeXT In 1993, NeXT worked with Sun to create OpenStep, an open specification of NeXTSTEP on Sun hardware 3

History (II) In 1997, Apple purchased NeXT and transformed NeXTSTEP into MacOS X which was first released in the summer of 2000 Objective-C has been one of the primary ways to develop applications for MacOS for the past 11 years In 2008, it became the primary way to develop applications for ios targeting (currently) the iphone and the ipad and (soon, I m guessing) the Apple TV 4

Objective-C is C plus Objects (I) Objective-C makes a small set of extensions to C which turn it into an object-oriented language It is used with two object-oriented frameworks The Foundation framework contains classes for basic concepts such as strings, arrays and other data structures and provides classes to interact with the underlying operating system The AppKit contains classes for developing applications and for creating windows, buttons and other widgets 5

Objective-C is C plus Objects (II) Together, Foundation and AppKit are called Cocoa On ios, AppKit is replaced by UIKit Foundation and UIKit are called Cocoa Touch In this lecture, we focus on the Objective-C language, we ll see a few examples of the Foundation framework we ll see examples of UIKit in Lecture 13 6

C Skills? Highly relevant Since Objective-C is C plus objects any skills you have in the C language directly apply statements, data types, structs, functions, etc. What the OO additions do, is reduce your need on structs, malloc, dealloc and the like and enable all of the object-oriented concepts we ve been discussing Objective-C and C code otherwise freely intermix 7

Development Tools (I) Apple provides XCode and Interface Builder to develop programs in Objective-C Behind the scenes, XCode makes use of gcc to compile Objective-C programs In a future version of XCode, to be released shortly, Interface Builder will go away as a separate application Its functionality will be merged into XCode We ll see examples of Interface Builder next week 8

Development Tools (II) XCode and Interface Builder comes with Snow Leopard on the Development Tools DVD It is also available for download at <http:// developer.apple.com/> You need to register in Apple s development program but registration is free To install: double click the downloaded.dmg file and then double click the installer and follow instructions 9

Hello World As is traditional, let s look at our first objective-c program via the traditional Hello World example To create it, we launch XCode and create a New Project In the resulting dialog (see next slide) select Application under the MacOS X select Command Line Tool on the right select Foundation from the pop-up menu click Choose and select a location for the project 10

11

Similar to what we saw with Eclipse, XCode creates a default project for us; There are folders for this command line program s source code (.m files), documentation, external dependencies and products (the application itself) Note: the Foundation framework is front and center and HelloWorld is shown in red because it hasn t been created yet 12

Exciting, isn t it. The template is ready to run; clicking Build and Run brings up a console that shows Hello, World! being displayed; One interesting thing to note is that the program is being run by gdb 13

The resulting project structure on disk does not map completely to what is shown in Xcode; The source file, man page, project file, and precompiled header file are all in the same directory; A lot of stuff gets generated when you build 14

The resulting executable can be executed from the command line, fulfilling the promise that we were creating a command-line tool As you can see, most of the text on Slide 11 was generated by gdb our command line tool doesn t do much but say hi to the world. Note the 2011-02-17 08:51:21.023 HelloWorld[41071:903] is generated by a function called NSLog() as we ll see next 15

Objective-C programs start with a function called main, just like C programs; #import is similar to C s #include except it ensures that header files are only included once and only once Ignore the NSAutoreleasePool stuff for now Thus our program calls a function, NSLog, and returns 0 The blue arrow indicates that a breakpoint has been set; gdb will stop execution on line 7 the next time we run the program 16

gdb is integrated into XCode; here we see the program stopped at our breakpoint; variables can be viewed in the upper right 17

Let s add objects Note: This example comes from Learning Objective-C 2.0: A Hands-On Guide to Objective-C for Mac and ios Developers written by Robert Clair It is an excellent book that I highly recommend His review of the C language is an excellent bonus to the content on Objective-C itself We re going to create an Objective-C class called Greeter to make this HelloWorld program a bit more objectoriented 18

First, we are going to add a class Select File New File In the resulting Dialog (see next two slides) Select Cocoa Class Select Objective-C class Select NSObject from the Subclass of menu Click the Next button and title the class Greeter.m and ask that Greeter.h be generated. Click Finish 19

20

This dialog names the new class, creates a header file and adds the class to the current project (HelloWorld) and to the current target (HelloWorld); A single project can generate multiple executables, which are known as targets; for this program, we just have one target 21

After the creation, Greeter.h and Greeter.m have been added to our project and the header file has been displayed automatically; Note the import of Cocoa.h; all this does is import Foundation.h and AppKit.h. We could switch this to just import Foundation.h as we won t be using AppKit 22

Objective-C classes Classes in Objective-C are defined in two files A header file which defines the attributes and method signatures of the class An implementation file (.m) that provides the method bodies 23

Header Files The header file of a class has the following structure <import statements> @interface <classname> : <superclass name> { <attribute definitions> } <method signature definitions> @end 24

Objective-C additions to C (I) Besides the very useful #import, the best way to spot an addition to C by Objective-C is the presence of this symbol @ 25

Objective-C additions to C (II) In header files, the two key additions from Objective-C are and @interface @end @interface is used to define a new objective-c class As we saw, you provide the class name and its superclass; Objective-C is a single inheritance language @end does what it says, ending the @interface compiler directive 26

Greeter s interface (I) We ve added one attribute greetingtext of type NSString * pointer to an NSString NS stands for NeXTSTEP!! 27

Greeter s interface (II) We ve added three method signatures one getter, one setter, and one method to issue a greeting 28

Objective-C Methods (I) It takes a while to get use to Object-C method signatures - (void) setgreetingtext: (NSString *) newtext; defines an instance method (-) called setgreetingtext: The colon signifies that the method has one parameter and is PART OF THE METHOD NAME newtext of type (NSString *) The names setgreetingtext: and setgreetingtext refer to TWO different methods; the former has a parameter 29

Objective-C Methods (II) A method with multiple parameters will have multiple colon characters and the parameter defs are interspersed with the method name - (void) setstrokecolor: (NSColor *) strokecolor andfillcolor: (NSColor *) fillcolor; The above signature defines a method with two parameters called setstrokecolor:andfillcolor: 30

NSString * and NSColor * We ve now seen examples of types NSString * and NSColor * What does this mean? The * in C means pointer Thus, this can be read as pointer to <class> it simply means an instance has been allocated and we have a handle to the instance 31

Let s implement the method bodies The implementation file of a class looks like this <import statements> @implementation <classname> <method body definitions> @end 32

Greeter s implementation We implement the getter, setter, greet method and a Framework method called dealloc that takes care of memory allocation issues The getter is straightforward; Let s look at the others in detail 33

But first, calling methods (I) The method invocation syntax of Objective-C is [object method:arg1 method:arg2 ]; Method calls are enclosed by square brackets Inside the brackets, you list the object being called Then the method with any arguments for the methods parameters 34

But first, calling methods (II) Here s a call using Greeter s setter method; @ Howdy! is a shorthand syntax for creating an NSString instance [greeter setgreetingtext: @ Howdy! ]; Here s a call to the same method where we get the greeting from some other Greeter object [greeterone setgreetingtext:[greetertwo greetingtext]]; Above we nested one call inside another; now a call with multiple args [rectangle setstrokecolor: [NSColor red] andfillcolor: [NSColor green]]; 35

Memory Management (I) Memory management of Objective-C objects involves the use of six methods alloc, init, dealloc, retain, release, autorelease Objects are created using alloc and init We then keep track of who is using an object with retain and release We get rid of an object with dealloc (although, we never call dealloc ourselves) 36

Memory Management (II) When an object is created, its retain count is set to 1 It is assumed that the creator is referencing the object that was just created If another object wants to reference it, it calls retain to increase the reference count by 1 When it is done, it calls release to decrease the reference count by 1 If an object s reference count goes to zero, the runtime system automatically calls dealloc 37

Memory Management (III) I won t talk about autorelease today, we ll see it in action soon Objective-C 2.0 added a garbage collector to the language When garbage collection is turned on, retain, release, and autorelease become no-ops, doing nothing However, the garbage collector is not available when running on ios, so the use of retain and release are still with us; as the hardware of ios devices gets more powerful, garbage collection will be available everywhere and these memory management techniques will go away 38

Back to the Code - (void) setgreetingtext: (NSString *) newtext { } [newtext retain]; [greetingtext release]; greetingtext = newtext; You are now in a position to understand the setter method We retain the new NSString passed in, we release the NSString we previously pointed at, we set our greetingtext to point at the new NSString 39

The greet method - (void) greet { } NSLog(@"%@", [self greetingtext]); NSLog is a variable argument function. The number of arguments is determined by the format string that is passed in as the first argument Just like printf in C This format string %@ says convert an object into a string Note: [self greetingtext] is this object invoking the getter function on itself rather than accessing the greetingtext string directly 40

The dealloc method - (void) dealloc { } [greetingtext release]; [super dealloc]; The dealloc method releases the NSString that we are pointing at with our attribute and then invokes the dealloc method of our superclass We ve now seen examples of the self and super keywords 41

A new main method We now need a new version of main to make use of our new Greeter class We ll import its header file We ll instantiate an instance of the class We ll set its greeting text We ll call its greet method We ll release it 42

The only thing new is the sequence of calls on alloc and init; alloc is a class method of NSObject; we can invoke it on Greeter since it is inherited; it returns a new instance and we then call init on it. We didn t override init so a default version defined by NSObject will execute instead. Otherwise, we create it, set the greeting, invoke greet, and release; ignore pool for now 43

Some things not (yet) discussed Objective-C has a few additions to C not yet discussed The type id: id is defined as a pointer to an object id icanpointatastring = @ Hello ; Note: no need for an asterisk in this case The keyword nil: nil is a pointer to no object It is similar to Java s null The type BOOL: BOOL is a boolean type with values YES and NO; used throughout the Cocoa frameworks 44

Wrapping Up (I) Basic introduction to Objective-C main methods class and method definition and implementation method calling syntax creation of objects and memory management More to come as we use this knowledge to explore the ios platform in future lectures 45

Coming Up Next Homework 4 Assigned on Friday Lecture 13: Introduction to ios Homework 4 Due on Wednesday Lecture 14: Review for Midterm Lecture 15: Midterm Lecture 16: Review of Midterm 46