Objectives:
- To understand the class structure and its implementation in Objective-C
- To get acquainted with Objective-C syntax
Class Structure
- Interface Section - where your data components and function headers are located; found in .h files
- Implementation Section - where your implementation for your functions are located; found in .m files
- Program Section - these are your codes; where your instatiate objects to carry out your tasks
Class Definition and Instatiation Syntax
Interface (.h files)
@interface NameOfClass:ParentClass {
//instance variables
returnType nameOfVariable;
}
//instance methods
- (returnType)methodName1; //No Parameters
- (returnType)methodName2:(returnType)parameter; //With One Parameter
- (returnType)methodName3:(returnType)parameter1 ThisIsPartOfTheMethodNameWithData:(returnType)parameter2; //With MultipleParameters
@end
Implementation (.m files)
@implementation NameOfClass
- (returnType)methodName1 {
//Put your codes here
}
- (returnType)methodName2:(returnType)parameter {
//Put your codes here
}
- (returnType)methodName3:(returnType)parameter1 ThisIsPartOfTheMethodNameWithData:(returnType)parameter2 {
//Put your codes here
}
@end
Program
main.m file //Remeber to import the class you created
int main(int argc, char *argv[]) {
//Create instance of the class
NameOfClass *instanceName1 = [[NameOfClass alloc] init];
//Or you may do it this way
NameOfClass *instanceName2;
instanceName2 = [NameOfClass alloc];
instanceName2 = [NameOfClass init];
//Calling a class function
[instanceName2 methodName];
[instanceName2 methodName2:100]; //assume that the dataType of the parameter is int, let's pass integer 100
//If you no longer need the objects, release their memory
[instanceName1 release];
[instanceName2 release];
}
I'm just learning here, but shouldn't the example Program do
ReplyDeleteinstanceName2 = [NameOfClass alloc];
instanceName2 = [instanceName2 init];
very informative
ReplyDeletehttp://quincetravels.com