iPhone applications are developed in Objective C language. This language is particularly created for mobile devices development. There are lots of similarities between C Language and Objective-C Language some of them mentioned below:

  • Strict superset of C – everything that works in C works in Objective-C (unlike C++)
  • Super-set because it adds object-oriented constructs to the language
  • When compiling Objective-C, it largely get turned into C
  • Derives a lot of design and functionality from Smalltalk


Objective C types

NSUinteger and NSInteger

  • Use instead of int, long, unsigned, etc
  • For architecture independence


  • Wrapper for C-String
  • Easiest way to create them is @”My String”


  • Objective-C boolean
  • Valid values are YES and NO


Calling Methods




[object method];
[object methodWithInput:input];
output = [object methodWithInputAndOutput:input];

Term used in Objective-C for calling method is “Sending Message”.


Nested Messages

Many languages:

function1 ( function2() );


[objectX function1:[objectX function2]];


Multi-Input Methods

Many languages

void function (int arg1, int arg2);

Method Call:

function(10, 20);


(void) functionWithArg1:(int)arg1 Arg2:(int)arg2;

Method Call:

[object functionWithArg1:10 Arg2:20];


Basic Memory Management

Objective-C has a Garbage Collection system as well as there are functions for Manual Memory Management.

  • alloc memory
  • release memory
  • autorelease memory

Example (alloc and release):

NSString *myString = [[NSString alloc] init];
[myString release];

Example (autorelease):

NSString *myString = [[[NSString alloc] init] autorelease];

Example (autorelease, convenience method);

NSString *myString = [NSString string];


Managing an instance variable

- (void)setTotalAmount: (NSNumber*)input {
[input retain];
[totalAmount release];
totalAmount = input;
- (void)dealloc {
[totalAmount release];
[super dealloc];


Creating Objects (NSString)

NSString* myString = @"My String";
NSString* myString = [NSString string];
NSString* myString = [[NSString alloc] init];
NSString* anotherString = [NSString stringWithFormat:@"%d %s", 1, @"String"];
NSString* fromCString = [NSString stringWithCString:"A C string" encoding:NSASCIIStringEncoding];


Weak versus Strong typing

  • Example of strong and weak typing for variables:
    • MyClassA *myObjectA; // Strong typing
    • MyClassB *myObjectB; // Strong typing
    • id myObject; // Weak typing
    • myObject = myObjectA; // No problem
    • myObject = myObjectB; // No problem
  • “id” is not a class, it’s a type
  • Typing object as id tells compiled: “This will eventually become Objective-C object”
  • Powerful because you can send any message to an id object and nothing happens till the runtime



NSObject is the root class of most. Objective-C class hierarchies. Through NSObject, objects inherit a basic
interface to the runtime system and the ability to behave as Objective-C objects.


Designing a Class Interface

#import <Cocoa/Cocoa.h>
@interface Photo : NSObject {
NSString* caption;
NSString* photographer;
- (NSString*) caption;
- (NSString*) photographer;
- (void) setCaption: (NSString*)input;
- (void) setPhotographer: (NSString*)input;


Class Implementation

#import "Photo.h"
@implementation Photo
(NSString*)caption {
return caption;
- (NSString*)photographer {
return photographer;
- (void)setCaption:(NSString*)input {
caption = [input retain];
- (void)setPhotographer:(NSString*)input {
[photographer autorelease];
photographer = [input retain];
- (id)init {
if ( self = [super init] ) {
[self setCaption:@"Default Caption"];
[self setPhotographer:@"Default Photographer"];
return self;
- (id)initWithCaption:(NSString *)caption
Photographer:(NSString)photographer {
if ( self = [super init] ) {
[self setCaption:caption];
[self setPhotographer:photographer];
return self;
+ (Photo *)newPhotoWithCaption:(NSString *)caption
Photographer:(NSString)photographer {
return [[Photo alloc] initWithCaption:caption
+ (Photo *)photoWithCaption:(NSString *)caption
Photographer:(NSString *)photographer {
return [[[Photo alloc] initWithCaption:caption
Photographer:photograhper] autorelease];
- (void)dealloc {
[photographer release];
[super dealloc];



Logging messages to the console in Objective-C:

NSLog (@"The current date is: %@.", [NSDate date] );



Automatically generate getters and setters
Photo example using properties:

#import <Cocoa/Cocoa.h>
@interface Photo : NSObject {
NSString* caption;
NSString* photographer;
@property (retain) NSString* caption;
@property (retain) NSString* photographer;




#import <Cocoa/Cocoa.h>
@interface NSString (Utility)
- (BOOL)isURL;



Selectors identify methods by name. Conceptually similar to function pointer


SEL setWidthHeight = @selector(setWidth:height:);

Here, the selector for setWidth:height: is assigned to the setWidthHeight variable.



Consistent way of enumeration over objects in collections. Use with NSArray, NSDictionary, NSSet, etc.
Example:  NSArray *people = …;

// old school method
Person *person;
int count = [array count];
for (int i=0; i<count; i++) {
person = [array objectAtIndex:i];
NSLog([person description]);
// new method (using enumeration)
for (Person *person in array) {
NSLog([person description]);


Class Introspection

You can ask any object about its class:

NSString *myObject = [NSString string];
NSLog(@"Name of my class is %@", [myObject className]);

Testing for general class membership

(subclasses included):
if ([myObject isKindOfClass:[UIControl class]]) {
// … do something

Testing for specific class membership

(subclasses excluded):
if ([myObject isMemberOfClass:[NSString class]]) {
// … do something specific to NSString


Mutable vs. Immutable

Most Foundation-level classes have two forms:

  • Cannot be changed
  • Efficient both in memory and speed (most of the times)
  • Naming: NSString, NSArray etc
  • Obtain immutable copy by calling copy


  • Changeable after allocation
  • Mutability adds overhead for saving and usually access as well
  • Naming: NSMutableString, NSMutableArray etc
  • Obtain mutable copy by calling mutableCopy

NSString vs. NSMutableString
Collection Classes:

  • NSArray vs. NSMutableArray
  • NSDictionary vs. NSMutableDictionary
  • NSData vs. NSMutableData
  • NSSet vs. NSMutableSet
Tagged with: C/C++ languageObject Oriented

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Related News Feeds

Set your Twitter account name in your settings to use the TwitterBar Section.