Objective-C Object Oriented Programming. Overview Objective-C is an object oriented language....

26
Objective-C Object Oriented Programming

Transcript of Objective-C Object Oriented Programming. Overview Objective-C is an object oriented language....

Page 1: Objective-C Object Oriented Programming. Overview Objective-C is an object oriented language. follows ANSI C style coding with methods from Smalltalk.

Objective-C

Object Oriented Programming

Page 2: Objective-C Object Oriented Programming. Overview Objective-C is an object oriented language. follows ANSI C style coding with methods from Smalltalk.

Overview

Objective-C is an object oriented language. follows ANSI C style coding with methods

from Smalltalk There is no formal written standard

Relies mostly on libraries written by others Flexible almost everything is done at runtime.

Dynamic Binding Dynamic Typing Dynamic Linking

Page 3: Objective-C Object Oriented Programming. Overview Objective-C is an object oriented language. follows ANSI C style coding with methods from Smalltalk.

Inventors

Objective-C was invented by two men, Brad Cox and Tom Love.

Both were introduced to Smalltalk at ITT in 1981

Cox thought something like Smalltalk would be very useful to application developers

Cox modified a C compiler and by 1983 he had a working Object-oriented extension to C called OOPC.

Page 4: Objective-C Object Oriented Programming. Overview Objective-C is an object oriented language. follows ANSI C style coding with methods from Smalltalk.

Development

Tom Love acquired a commercial copy of Smalltalk-80 while working for Schlumberger Research

With direct access Smalltalk, Love added more to OOPC making the final product, Objective-C.

In 1986 they release Objective-C through their company “Stepstone”

Page 5: Objective-C Object Oriented Programming. Overview Objective-C is an object oriented language. follows ANSI C style coding with methods from Smalltalk.

NeXT and NeXTSTEP

In 1988 Steve Jobs acquires Objective-C license for NeXT

Used Objective-C to build the NeXTSTEP Operating System

Objective-C made interface design for NeXTSTEP much easier

NeXTSTEP was derived from BSD UnixIn 1995 NeXT gets full rights to

Objective-C from Stepstone

Page 6: Objective-C Object Oriented Programming. Overview Objective-C is an object oriented language. follows ANSI C style coding with methods from Smalltalk.
Page 7: Objective-C Object Oriented Programming. Overview Objective-C is an object oriented language. follows ANSI C style coding with methods from Smalltalk.
Page 8: Objective-C Object Oriented Programming. Overview Objective-C is an object oriented language. follows ANSI C style coding with methods from Smalltalk.

OPENSTEP API

Developed in 1993 by NeXT and SunAn effort to make NeXTSTEP-like

Objective-C implementation available to other platforms.

In order to be OS independent Removed dependency on Mach Kernel Made low-level data into classes

Paved the way for Mac OS X, GNUstep

Page 9: Objective-C Object Oriented Programming. Overview Objective-C is an object oriented language. follows ANSI C style coding with methods from Smalltalk.

Apple and Mac OS X

NeXT is taken over by Apple in 1996 (294 million) and put Steve Jobs back at Apple.

Redesigned Mac OS to use objective-C similar to that of NeXTSTEP

Developed a collection of libraries named “Cocoa” to aid GUI development

Release Mac OS X (ten), which was radically different than OS 9, in March 2001

Page 10: Objective-C Object Oriented Programming. Overview Objective-C is an object oriented language. follows ANSI C style coding with methods from Smalltalk.

The Cocoa API

Primarily the most frequently used frameworks nowadays.

Developed by Apple from NeXTSTEP and OPENSTEP

Has a set of predefined classes and types such as NSnumber, NSstring, Nsdate, etc.

NS stands for NeXT-sun Includes a root class NSObject where words

like alloc, retain, and release come from

Page 11: Objective-C Object Oriented Programming. Overview Objective-C is an object oriented language. follows ANSI C style coding with methods from Smalltalk.

Dynamic Language

Almost everything is done at runtimeUses dynamic typing, linking, and

bindingThis allows for greater flexibilityMinimizes RAM and CPU usage

Page 12: Objective-C Object Oriented Programming. Overview Objective-C is an object oriented language. follows ANSI C style coding with methods from Smalltalk.

To Import or Include?

C/C++’s #include will insert head.h into the code even if its been added before.

Obj-C’s #import checks if head.h has been imported beforehand.

#import head.h

Page 13: Objective-C Object Oriented Programming. Overview Objective-C is an object oriented language. follows ANSI C style coding with methods from Smalltalk.

Messages

Almost every object manipulation is done by sending objects a message

Two words within a set of brackets, the object identifier and the message to send.

Because of dynamic binding, the message and receiver are joined at runtime

[Identifier message ]

Page 14: Objective-C Object Oriented Programming. Overview Objective-C is an object oriented language. follows ANSI C style coding with methods from Smalltalk.

Basic syntax structure

C++ syntax

Objective-C syntax

void function(int x, int y, char z);

Object.function(x, y, z);

-(void) function:(int)x, (int)y, (char)z;

[Object function:x, y, z];

Page 15: Objective-C Object Oriented Programming. Overview Objective-C is an object oriented language. follows ANSI C style coding with methods from Smalltalk.

Keyword: id

The word ‘id’ indicates an identifier for an object much like a pointer in c++

This uses dynamic typingFor example, if Pen is a class…

extern id Pen;

id myPen;

myPen = [Pen new ];(Cox, 59)

Page 16: Objective-C Object Oriented Programming. Overview Objective-C is an object oriented language. follows ANSI C style coding with methods from Smalltalk.

Memory Allocation

Objects are created dynamically through the keyword, “alloc”

Objects are dynamically deallocated using the words “release” and “autorelease”

autorelease dealocates the object once it goes out of scope.

NOTE: None of these words are built-in

Page 17: Objective-C Object Oriented Programming. Overview Objective-C is an object oriented language. follows ANSI C style coding with methods from Smalltalk.

Ownership

Objects are initially owned by the id that created them.

Like C++ pointers, multiple IDs can use the same object.

However, like in C++ if one ID releases the object, then any remaining pointers will be referencing invalid memory.

A method like “retain” can allow the object to stay if one ID releases it.

Page 18: Objective-C Object Oriented Programming. Overview Objective-C is an object oriented language. follows ANSI C style coding with methods from Smalltalk.

Prototyping functions

When declaring or implementing functions for a class, they must begin with a + or -

+ indicates a “class method” that can only be used by the class itself. In other words, they’re for private functions.

- indicates “instance methods” to be used by the client program (public functions).

Page 19: Objective-C Object Oriented Programming. Overview Objective-C is an object oriented language. follows ANSI C style coding with methods from Smalltalk.

Class Declaration (Interface)

#import <Cocoa/Cocoa.h>@interface Node : NSObject {

Node *link;int contents;

}+(id)new;-(void)setContent:(int)number;-(void)setLink:(Node*)next;-(int)getContent;-(Node*)getLink;@end

node.h

Page 20: Objective-C Object Oriented Programming. Overview Objective-C is an object oriented language. follows ANSI C style coding with methods from Smalltalk.

Class Definition (Implementation)

#import "node.h”@implementation Node+(id)new

{ return [Node alloc];}-(void)setContent:(int)number

{contents = number;}-(void)setLink:(Node*)next {

[link autorelease];link = [next retain];

}-(int)getContent

{return contents;}-(Node*)getLink

{return link;}@end

node.m

Page 21: Objective-C Object Oriented Programming. Overview Objective-C is an object oriented language. follows ANSI C style coding with methods from Smalltalk.

C++ VS. Objective-C

Adds OOP, metaprogramming and generic programming to C

Comes with a std library

Has numerous uses Large and complex

code for OOP

Only adds OOP to C Has no standard

library; is dependant on other libraries

Mostly used for application building

Simpler way of handling classes and objects

Page 22: Objective-C Object Oriented Programming. Overview Objective-C is an object oriented language. follows ANSI C style coding with methods from Smalltalk.

Objective-C 2.0

In October 2007, Apple Inc. releases Objective-C 2.0 for Mac OS 10.5 (Leopard)

Adds automatic garbage collection Instance Methods (public functions) are

defined differently using @property

Page 23: Objective-C Object Oriented Programming. Overview Objective-C is an object oriented language. follows ANSI C style coding with methods from Smalltalk.

linkList class

#import "linkList.h"@implementation linkList+(id)new {return [linkList alloc];}-(void)insert:(int)value { id temp = [Node new]; [temp setContent:value]; [temp setLink:head]; head = [temp retain]; [temp release];}

-(void)append:(int)value { id last = [head getLink]; while ([last getLink] != nil) {last = [last getLink];} id temp = [Node new]; [temp setContent:value]; [last setLink:temp]; [temp release];}-(void)remove { id temp = head; head = [head getLink]; [temp release];}-(int)getValue { return [head getContent];}@end

linkList.m

Page 24: Objective-C Object Oriented Programming. Overview Objective-C is an object oriented language. follows ANSI C style coding with methods from Smalltalk.

Stack class

#import "linkList.h”@interface Stack : linkList{}+(id)new;-(void)push:(int)value;-(int)pop;@end

#import "stack.h”

@implementation Stack+(id)new {return [Stack alloc];}

-(void)push:(int)value {[self insert:value];}

-(int)pop { int ret = [self getValue]; [self remove]; return ret;}

@endstack.h stack.m

Page 25: Objective-C Object Oriented Programming. Overview Objective-C is an object oriented language. follows ANSI C style coding with methods from Smalltalk.

Example: main.c

#import "stack.h”int main(){

Stack *s = [Stack new];[s push:1];[s push:2];printf("%d\t", [s pop]);[s push:3];printf("%d\t", [s pop]);printf("%d\t", [s pop]);[s release];return 0;

}

$ gcc -x objective-c node.m linkList.m stack.m main.c -framework Cocoa -o stackTest

$./stackTest

2 3 1

main.c

Page 26: Objective-C Object Oriented Programming. Overview Objective-C is an object oriented language. follows ANSI C style coding with methods from Smalltalk.

References

Cox, Brad. Object Oriented Programming: an Evolutioary Approach

Sebesta, Robert. Concepts of Programming Languages

Apple Inc. Apple Developer Connectionhttp://developer.apple.com

Stevenson, Scott. Theocacaohttp://theocacao.com/document.page/510

Various Authors. Wikipedia: the Free Encyclopedia

http://en.wikipedia.org