Home > Tutorials > Making the Transition to Objective C

Making the Transition to Objective C

April 24th, 2009

Objective C is like any other programming language, however it's syntax is a lot different from standards you'll see used in Vanilla C, C++ and Java. The good news is Objective C is simply a super set of C, meaning that all vanilla C code (and most C++ I have tried) compiles fine on XCode and within the same projects as Objective C. Having said that you can't rely on C alone to build iPhone applications, you must use Objective C for the vast majority of frameworks. The easiest way to understand Objective C objects is to build you own, and that's what I aim to show you.

Here is a typical Objective C class:

// MyFirstClass.h
#import <Foundation/Foundation.h>
 
@interface MyFirstClass : NSObject
{
}
 
@end
// MyFirstClass.m
#import "MyFirstClass.h"
 
@implementation MyFirstClass
 
@end

This is a skeleton class, which we will fill with our useful instance variables and functions. Notice how I added NSObject as the inherited class, this automatically adds alloc functions to your class, without you having to code them personally. Most classes will need instance variables, and there are 2 types of variables, public and private. In Objective C it isn't as simple as declaring public or private before the variable, each variable is assumed private until it is 'synthesized'. Now I will show you a class with 2 instance variables, one being private and the other being public.

// MyFirstClass.h
#import <Foundation/Foundation.h>
 
@interface MyFirstClass : NSObject
{
	int myPrivateInt;
	int myPublicInt;
}
 
@property int myPublicInt;
 
@end
// MyFirstClass.m
#import "MyFirstClass.h"
 
@implementation MyFirstClass
 
@synthesize myPublicInt;
 
@end

You'll note that the public variable differs from the private variable by 2 declarations. One is the property declaration, that ties it publicly to the 'structure' of the class. The other is the synthesize declaration, all this does is virtually define the get/set methods of the variable. Something important to note is that if you have a variable that is a pointer to an object, you will have to define the property like so:

@property (nonatomic,retain) NSObject* object;

Now we have to distinguish between public and private functions within the object. Again there isn't a simple private or public declaration, instead you define the prototypes of public functions in the header, and the prototypes of private functions in the main file, like so:

// MyFirstClass.h
#import <Foundation/Foundation.h>
 
@interface MyFirstClass : NSObject
{
	int myPrivateInt;
	int myPublicInt;
}
 
@property int myPublicInt;
 
-(int) theIntPlus:(int)anotherInt;
 
@end
// MyFirstClass.m
#import "MyFirstClass.h"
 
@interface MyFirstClass()
-(void) setPrivateInt:(int)theInt;
@end
 
@implementation MyFirstClass
 
@synthesize myPublicInt;
 
-(int) theIntPlus:(int)anotherInt
{
	[self setPrivateInt:anotherInt];
	return myPublicInt+anotherInt;
}
 
-(void) setPrivateInt:(int)theInt
{
	myPrivateInt = theInt;
}
 
@end

As you can see, the public function (theIntPlus) is defined the header file, which is used as a reference to other objects, while the private functions prototype is only defined in the main file, which only the object itself uses. You may be wondering what the - symbol denotes on the functions, it represents a non-static function, while the + symbol represents a static function. I will show you a use of the static function for sharing classes.

// MyFirstClass.h
#import <Foundation/Foundation.h>
 
@interface MyFirstClass : NSObject
{
	int myPrivateInt;
	int myPublicInt;
}
 
@property int myPublicInt;
 
-(int) theIntPlus:(int)anotherInt;
+(id) sharedMyFirstClass;
 
@end
// MyFirstClass.m
#import "MyFirstClass.h"
 
MyFirstClass* shared_MyFirstClass = nil;
 
@interface MyFirstClass()
-(void) setPrivateInt:(int)theInt;
@end
 
@implementation MyFirstClass
 
@synthesize myPublicInt;
 
-(int) theIntPlus:(int)anotherInt
{
	[self setPrivateInt:anotherInt];
	return myPublicInt+anotherInt;
}
 
-(void) setPrivateInt:(int)theInt
{
	myPrivateInt = theInt;
}
 
+(id) sharedMyFirstClass
{
	if(shared_MyFirstClass == nil)
		shared_MyFirstClass = [MyFirstClass alloc];
	return shared_MyFirstClass;
}
 
@end

As you can see the + symbol allows you to access sharedMyFirstClass without allocating the object, and sharing the class allows you to access the same class throughout your program, which is very useful for some objects (such as Audio Recording classes). Lastly I'd like to draw your attention to how I have accessed the private function setPrivateInt, I got the instance variable and then defined the function I'm accessing within the [] brackets. This goes for any instance of any class, whether it be your own (self) or another one you have allocating.

I hope this explains the differences between Objective C and other more syntax orthodox languages.

Author: admin Categories: Tutorials Tags:
  1. No comments yet.
  1. No trackbacks yet.