Objective c
1 / 29

Objective-C - PowerPoint PPT Presentation

  • Uploaded on

Objective-C. First Part adapted from a presentation by Kevin Layer. Main Points. A Bit of History Main Features Some Examples. Overview. Objective-C is an object oriented language. follows ANSI C style coding with methods from Smalltalk There is no formal written standard

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about ' Objective-C' - moke

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Objective c


First Part adapted from a presentation by Kevin Layer

Main points
Main Points

A Bit of History

Main Features

Some Examples


  • 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


  • 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.


  • 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”

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 Unix

  • In 1995 NeXT gets full rights to Objective-C from Stepstone

Openstep api

  • Developed in 1993 by NeXT and Sun

  • An 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

Apple and mac os x
Apple and Mac OS X

  • NeXT is taken over by Apple in 1996 and put Steve Jobs and his Objective-C libraries to work

  • 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

The cocoa api
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

Dynamic language
Dynamic Language

  • Almost everything is done at runtime

  • Uses dynamic typing, linking, and binding

  • This allows for greater flexibility

  • Minimizes RAM and CPU usage

To import or include
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.hhas been imported beforehand.

#import head.h


  • 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 ]

Basic syntax structure
Basic syntax structure

C++ syntax

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

Object.function(x, y, z);

Objective-C syntax

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

[Object function:x, y, z];

Keyword id

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

This uses dynamic typing

For example, if Pen is a class…

Keyword: id

id myPen;

myPen = [Pen new ];

Memory allocation
Memory Allocation like a pointer in

  • 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

Ownership like a pointer in

  • 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.

Prototyping functions
Prototyping functions like a pointer in

  • 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).

Class declaration interface
Class Declaration (Interface) like a pointer in

#import <Cocoa/Cocoa.h>

@interface Node : NSObject {

Node *link;

int contents;









Class definition i m plementation
Class Definition (I like a pointer in mplementation)

#import "node.h”

@implementation Node


{ return [Node alloc];}


{contents = number;}

-(void)setLink:(Node*)next {

[link autorelease];

link = [next retain];



{return contents;}


{return link;}



C vs objective c

Adds OOP, metaprogramming and generic programming to C like a pointer in

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

C++ VS. Objective-C

Objective c 2 0
Objective-C 2.0 like a pointer in

  • 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

Linklist class

#import "linkList.h" like a pointer in

@implementation linkList


{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];}


linkList class


Stack class

#import "linkList.h” like a pointer in

@interface Stack : linkList






#import "stack.h”

@implementation Stack


{return [Stack alloc];}


{[self insert:value];}

-(int)pop {

int ret = [self getValue];

[self remove];

return ret;



Stack class



Example main c

#import "stack.h” like a pointer in

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;


Example: main.c


Example 1
Example 1 like a pointer in

#import <Foundation/Foundation>

int main (intargc, const char * argv[]) {

NSString *testString;

testString = [[NSStringalloc] init];

testString = @"Here's a test string in testString!";

NSLog(@"testString: %@", testString);

return 0;


The interface
The Interface like a pointer in

@interface SimpleCar : NSObject {

NSString* make;

NSString* model;

NSNumber* vin;


// set methods

- (void) setVin: (NSNumber*) newVin;

- (void) setMake: (NSString*) newMake;

- (void) setModel: (NSString*) setModel;

- (void) setMake: (NSString*) newMake

Model: (NSString*) newModel;

// get methods

- (NSString*) make;

- (NSString*) model;

- (NSNumber*) vin;


Implementation like a pointer in

@implementation SimpleCar

// set methods

- (void) setVin: (NSNumber*) newVin{

[vin release];

vin = [[NSNumberalloc] init];

vin = newVin;


- (void) setMake: (NSString*) newMake{

[make release];

make = [[NSStringalloc] initWithString:newMake];


- (void) setModel: (NSString*) newModel{

[model release];

model = [[NSStringalloc] initWithString:newModel];


Implementation continued
Implementation Continued like a pointer in

- (void) setMake: (NSString*) newMake

Model: (NSString*) newModel{

// Reuse our methods from earlier

[self setMake:newMake];

[self setModel:newModel];

Implementation continued1
Implementation continued … like a pointer in

// get methods

- (NSString*) make {

return make;


- (NSString*) model {

return model;


- (NSNumber*) vin {

return vin;