about objects · objective-c basics morning session b objects, classes, and methods messages and...

54
About Objects Introduction to Objective-C SECTION 1 Developer–oriented training. www.About Objects .com Friday, October 9, 2009

Upload: others

Post on 13-Oct-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

About Objects

Introduction to Objective-CSECTION 1

Developer–oriented training.

www.AboutObjects.com

Friday, October 9, 2009

Page 2: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

About Objects

Reston, VA — Cupertino, CA

Training

ANSI C

Objective-C

iPhone SDK

Cocoa/Cocoa touch

Friday, October 9, 2009

Page 3: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

360iDev Denver

Thanks to Conference organizers

Tom Ortega

John Wilker

And everyone else who helped: Great job!

About Objects delighted to take part

Look forward to continued sponsorship of future events.

How about Washington, D.C. for future conference?

3

Friday, October 9, 2009

Page 4: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

About The Instructors

Jonathan Lehr

President, About Objects

Developer, author, speaker

Objective-C developer/trainer since 1991

4

Developer–oriented training.

Friday, October 9, 2009

Page 5: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

About You

Are you familiar with other dev technologies?

Kinds of iPhone features you like to learn to develop?

Have you been trying to learn some things on your own?

5

Friday, October 9, 2009

Page 6: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

Topics, Morning Day 1

Given the one-day format, we'll limit the scope.

Morning Session A

Using Xcode

Objective-C Basics

Morning Session B

Objects, Classes, and Methods

Messages and the Objective-C runtime system

Memory Management

Declared Properties

6

Friday, October 9, 2009

Page 7: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

Topics, Afternoon, Day 1

Afternoon Session A

Overview of UIKit and Interface Builder

Applications, Windows, Views

ViewControllers and UIControls

Afternoon Session B

TableViews, Delegates, and DataSources

IB Outlets and Actions

Multiple Nib Files

Customizing UI Components

7

Friday, October 9, 2009

Page 8: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

Overview

Friday, October 9, 2009

Page 9: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

© Copyright 2008 About Objects, Inc. All rights reserved worldwide.

What is Objective-C?

ANSI C + Object-Oriented extensions

Combines the best of static and dynamic languages

GNU C compiler compiles both C and Objective-C code

Apple donated Objective-C to the GNU open source project

9

Friday, October 9, 2009

Page 10: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

© Copyright 2008 About Objects, Inc. All rights reserved worldwide.

What are Cocoa and Cocoa touch?

Cocoa is Mac UI layer

Cocoa touch is Cocoa on the iPhone

Each contains two Objective-C frameworks

Foundation (collections, value types, utility classes, etc.)

UIKit (iPhone) or AppKit (Mac)

10

Friday, October 9, 2009

Page 11: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

Developer Tools

Friday, October 9, 2009

Page 12: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

XCode IDE

Build

Run (Simulator, device)

Debug (gdb)

Source code management (SCM)

Documentation

Friday, October 9, 2009

Page 13: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

Xcode

All major platforms

Mac OS X

iPhone OS

Features

Syntax-aware code editor with completion, etc.

Automates builds

Context-sensitive documentation browser

Friday, October 9, 2009

Page 14: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

Interface Builder

Visual GUI design tool

Doesn't generate code

Works with ‘Freeze-dried’ objects

Archived (serialized) in .nib files

Dynamically loaded

Objects deserialized at load time

Friday, October 9, 2009

Page 15: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

Instruments

Profiling

Performance Monitoring

Garage-Band 'multi-track' interface

Profiling

Performance Monitoring

Garage-Band 'multi-track' interface

Friday, October 9, 2009

Page 16: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

© Copyright 2008 About Objects, Inc. All rights reserved worldwide.

History of Objective-C

Smalltalk

Earliest OO language (circa 1972) — Allen Kay, Xerox PARC

Objective-C

Developed in early eights to address Smalltalk performance (Brad Cox)

Licensed in '85 by NeXT Computer

Steve Jobs, Avie Tevanian

Used in UI layer, dev tools, and apps.

16

Friday, October 9, 2009

Page 17: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

Friday, October 9, 2009

Page 18: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

User Interface, Circa 1996

Friday, October 9, 2009

Page 19: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

Apple/NeXT Merger, '96

Friday, October 9, 2009

Page 20: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

© Copyright 2008 About Objects, Inc. All rights reserved worldwide.

Objective-C at Apple

Apple acquires NeXT in 1996 for modern OS to replace Mac OS 9

Apple then ported OpenStep to Motorola's PowerPC architecture

Added Quartz 2D rendering engine to replace Adobe Display PostScript

Ported Mac Toolbox APIs (Carbon)

New OS released 2001 as OS X 10.0

20

Friday, October 9, 2009

Page 21: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

iPhone OS

Port of Mac OS X to mobile platform

First release (2.0b2) March, 2008

Friday, October 9, 2009

Page 22: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

Objective-C Basics

Friday, October 9, 2009

Page 23: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

© Copyright 2008 About Objects, Inc. All rights reserved worldwide.

Message Syntax

Square brackets for message expressions

Java:

myString.toString()

Objective-C

[myString description]

23

Friday, October 9, 2009

Page 24: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

© Copyright 2008 About Objects, Inc. All rights reserved worldwide.

Method Arguments

Arguments are delimited by colons

Java:

person.setFirstName("Fred");

Objective-C

[person setFirstName:@"Fred"];

24

Friday, October 9, 2009

Page 25: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

© Copyright 2008 About Objects, Inc. All rights reserved worldwide.

Constants

Constant string object different from constant string

Java:

"Hello"

Objective-C

@"Hello" // String object"Hello" // C string

25

Friday, October 9, 2009

Page 26: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

© Copyright 2008 About Objects, Inc. All rights reserved worldwide.

Object Data Types

Objective-C objects are dynamically allocated structs

Variable types are therefore pointers to struct defined by class

Java:

Employee emp = new Employee();

Objective-C

Employee *emp = [[Employee alloc] init];

Obj-C also provides generic object type, id

id emp2 = [[Employee alloc] init];

26

Friday, October 9, 2009

Page 27: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

© Copyright 2008 About Objects, Inc. All rights reserved worldwide.

Constructors vs. Creation Methods

No constructors; creation methods are just methods

Ordinary return statements provide more flexibility

Calls to super can occur anywhere within a method

Inheritance is straight-forward

Memory allocation and initialization are separate steps

Java:

Employee emp = new Employee();

Objective-C

Employee *emp = [[Employee alloc] init];

27

Friday, October 9, 2009

Page 28: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

© Copyright 2008 About Objects, Inc. All rights reserved worldwide.

Prefix vs. Package Path

Obj-C language doesn't provide namespaces

Frameworks and libraries use prefixes by convention to avoid collisions

Java:

java.lang.String s = new String("hello");

Objective-C

NSString *s = [[NSString alloc] initWithString:"hi"];

28

Friday, October 9, 2009

Page 29: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

© Copyright 2008 About Objects, Inc. All rights reserved worldwide.

Classes

Two separate files

Class declared in .h file

Methods implemented in .m file

Compiler directives

@interface ... @end

@implementation ... @end

Curly braces

Instance variable section inside curly braces

Methods defined outside curly braces

29

Friday, October 9, 2009

Page 30: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

Anatomy of a Class Declaration

@interface Person : NSObject { int _age; NSString * _firstName;}

...

@end

compiler directive

class we're declaring class it inherits from

name of instance variabledata type

Friday, October 9, 2009

Page 31: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

Class Declaration: Methods

@interface Person : NSObject { ... // Instance variables go here}

// Method declarations start here...

- (int)age;

- (void) setAge: (int)anAge;

...

@end

return type

method name

arg type

arg name

Friday, October 9, 2009

Page 32: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

© Copyright 2009 About Objects, Inc. All rights reserved worldwide.

#import <Foundation/Foundation.h>

// Following line declares the Person class as a subclass of NSObject

@interface Person : NSObject { // Instance Variables. Note: Underscore prefix is conventional but not required. int _age; NSString *_firstName;}

// Methods. Note: Getter methods are not prefixed with 'get'.

- (int)age; // Returns person's age.- (void)setAge:(int anAge); // Sets person's age to anAge.

- (NSString *)firstName; // Returns person's first name.- (void)setFirstName:(NSString *)aName; // Set person's first name to aName.

@end

Class Declaration

32

Friday, October 9, 2009

Page 33: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

© Copyright 2009 About Objects, Inc. All rights reserved worldwide.

#import "Person.h"

@implementation Person

- (int)age{ return _age;}

- (void)setAge:(int age){ _age = age;}

- (NSString *)firstName{ return firstName;}

- (void)setFirstName:(NSString *)firstName{ _firstName = firstName; // Note: This code omits some memory management details...}

@end

Class Implementation

33

Friday, October 9, 2009

Page 34: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

© Copyright 2008 About Objects, Inc. All rights reserved worldwide.

Method Prototypes

Methods declared in .h, implemented in .m

Data types enclosed in parens

Instance methods prefixed with -

Class methods prefixed with +

// Method declarations- (id)init;+ (id)alloc;

34

Friday, October 9, 2009

Page 35: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

© Copyright 2008 About Objects, Inc. All rights reserved worldwide.

No Method Overloading

Runtime system looks up methods by name rather than signature

Makes introspection simpler and more efficient

Java:

manager.addEmployee(emp);manager.addEmployee(emp, "Developer");

Objective-C

[manager addEmployee:emp];[manager addEmployee:emp withTitle:@"Developer"];

35

Friday, October 9, 2009

Page 36: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

© Copyright 2008 About Objects, Inc. All rights reserved worldwide.

Multi-Argument Methods

Method names can be composed of multiple sections

Each section ends with a colon that delimits the next arg

Java:

public void addEmployee(Employee emp, String title)

Objective-C

- (void)addEmployee:(Employee *)emp withTitle:(NSString *)title

Name of method is addEmployee:withTitle:

Args are emp and title

36

Friday, October 9, 2009

Page 37: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

© Copyright 2008 About Objects, Inc. All rights reserved worldwide.

Visibility Modifiers

Compiler directives

@private

@protected

@public

No visibility modifiers for methods

Methods made 'private' by omitting declarations from .h file.

To emphasize 'privacy', prefix method name with underscore.

37

Friday, October 9, 2009

Page 38: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

© Copyright 2009 About Objects, Inc. All rights reserved worldwide.

@interface Person : NSObject{

// Visibility modifier. Applies to all ivars that follow.@private // Private instance variables. NSString *_firstName; NSNumber *_salary;

@protected // Protected instance variables. int _age; }

// Instance methods....

@end

Using Visibility Modifiers

38

Friday, October 9, 2009

Page 39: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

Writing a Program

Friday, October 9, 2009

Page 40: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

© Copyright 2008 About Objects, Inc. All rights reserved worldwide.

World's Shortest Obj-C Program

What makes this Objective-C?

It's in a file named main.m

String constant prefixed with @ (which makes it a string object in Obj-C)

More than just an Objective-C program; it's a Foundation tool!

Imports Foundation.h

Calls Foundation's NSLog function (a C function)

40

#import <Foundation/Foundation.h>

int main(){ NSLog(@"Hello, World!");}

Friday, October 9, 2009

Page 41: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

© Copyright 2008 About Objects, Inc. All rights reserved worldwide.

Working with Xcode

Setting Preferences

Creating a Project

Adding Groups

Adding Files

Building and Fixing Compilation Errors

Running

41

Friday, October 9, 2009

Page 42: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

Preferences: IDE Layout

Select All-In-One. If drop down disabled, close any open projects !rst.

Friday, October 9, 2009

Page 43: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

Preferences: Building

Set to Always.

Friday, October 9, 2009

Page 44: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

Preferences: Debugging

Select Show Console & Debugger.

Friday, October 9, 2009

Page 45: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

Preferences: Fonts & Colors

➊ Click Duplicate.

➋ Click on a row and choose Select All from the menu.

➌ Type ⌘T to bring up Font Panel and click to select font size.

Friday, October 9, 2009

Page 46: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

Preferences: Indentation

➋ Select Always.

➊ Check Syntax-aware indenting.

Friday, October 9, 2009

Page 47: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

Preferences: SCM

➋ Fill in repository info.

➊ To set up SCM (not needed for this course), click + to add repositories.

➌ If using SSH, go to SSH tab to supply identities.

Friday, October 9, 2009

Page 48: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

Add New Group

➋ Select New Group...

➊ Right-click project name.

Friday, October 9, 2009

Page 49: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

Add New File

➋ Select New File...

➊ Right-click Group name.

➌ Select a !le template.

Friday, October 9, 2009

Page 50: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

Start Editing

Class declaration code generated from template.

.h and .m !les added automatically.

Friday, October 9, 2009

Page 51: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

Build

Inline error messages.

Click Build and Run when ready to build.

Compiler output.

Friday, October 9, 2009

Page 52: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

Run/Debug

Console.

Code editor. Debugger pane.

Friday, October 9, 2009

Page 53: About Objects · Objective-C Basics Morning Session B Objects, Classes, and Methods Messages and the Objective-C runtime system Memory Management Declared Properties 6 Friday, October

© Copyright 2008 About Objects, Inc. All rights reserved worldwide.

Lab 0

1. Create a project named Hello World

2. Build and Run

3. Change the ‘Hello World!’ message to ‘Farewell World!’ and build and run again

53

Friday, October 9, 2009