Create the new class
Menu > File > New File > iPhone OS > Cocoa Touch> Objective-C class
Subclass of: NSObject
Name it with your class name
Save it in a suitable directory within your project
Setting Up The Class With Basic Properites Etc
In the .h file
#import <Foundation/Foundation.h>
@interface MyClassName : NSObject //ClassName:SuperclassName
{
NSMutableArray *SomeArrayName;
NSMutableString *SomeStringName;
int SomeIntName;
BOOL SomeBoolName;
}
@property (nonatomic, retain) NSMutableArray *SomeArrayName;
@property (nonatomic, retain) NSMutableString *SomeStringName;
@property int SomeIntName;
@property BOOL SomeBoolName;
@end
In the .m file
Synthesize any class properties
@implementation MyClassName
@synthesize SomeArrayName, SomeStringName, SomeIntName, SomeBoolName;
An init function should have been created. In it initialise any managed objects
//**************************
//**************************
//********** INIT **********
//**************************
//**************************
- (id)init
{
self = [super init];
if (self)
{
// Initialization code here.
SomeArrayName = [[NSMutableArray alloc] init];
SomeStringName = [[NSMutableString alloc] init];
}
return self;
}
Add a dealloc function
//*****************************
//*****************************
//********** DEALLOC **********
//*****************************
//*****************************
- (void)dealloc
{
[SomeArrayName release];
[SomeStringName release];
[super dealloc];
}
Notes About The Class Elements
@interface
Used to declare a class in objective C
ClassName
The name you are calling the class
SuperclassName
The name of the classes superclass – only single inheritance is permitted so only 1 superclass can be used, E.g. NSObject
Inside Curley Braces
Declare instance variables
After Closing Curley Brace
Declare any of these in the following order:
properties (@property)
Class methods (start with ‘+’)
Instance methods (start with ‘-‘)
@end
Completes the declaration of the new class
Variables
Class instance variables are declared as normal inside the .h file curley braces, e.g.
NSString *SomeVariableName1;
int SomeVariableName2;
Creating Class Properties
To make a variable a property that may be accessed from outside the class add the @property declaration after the class closing curley brace (i.e. with any class method declarations):
}
@property (nonatomic, readonly) NSString *SomeVariableName1;
@property int SomeVariableName2;
The following property definitions may be used:
simple (default), copy, retain
How the setter method will set the variable.
readwrite (default), readonly
Can variable be changed
atomic (default), nonatomic
Does variable require a lock. Atomic=a lock must be acquired to get or set the varaible, nonatomic=no lock is required (and a bit faster).
You also need to ‘synthesize’ the properties inside the .m implementation file:
@implementation ClassName
@synthesize SomeVariableName1, SomeVariableName2;
@end
This tells the compiler to use the standard get and set methods, rather than you having to create the fucntions yourself.
.m Implementation File
@implementation ClassName
@end
All of the method definitions must be inside an implementation block.
Using the class
In the .h file where the class will be used
ClassName *ClassName1;
In the .m file init where the class will be used
ClassName1 = [[[self class] alloc] init];
Thanks for sharing this information..