Introduction to Objective-C

Objective-C is a programming language that allows object-oriented programming at a high level. It is an upgrade to the standard ANSI C programming language. Objective-C extends the standard ANSI C language by providing syntax for defining classes, methods and other constructs extension of classes. Before studying the Objective-C programming language, basic knowledge of programming in C programming language is required. Objective-C supports most standard object-oriented concepts with certain differences such as encapsulation, inheritance and polymorphism.

Objective-C and ANSI C

Objective-C is a programming language that represents the extension of ANSI C programming language and for that reason Objective-C retains the basic C syntax. Besides syntax, as in C programming language, header files and source files are used to separate public declarations from the implementation details of the code. Header files have .h extension, and source files have the .m extension, if it is only Objective-C syntax, and .mm extension, if it is a combination of Objective-C and C / C + + syntax.

Classes

As with most other object-oriented programming languages, classes in Objective-C programming language represent a description of some data structures with certain methods that can be applied to that data structure. The class defines two components: the interface and implementation. Class interface contains the class declarations, it contains definitions of the variables and methods. Class implementation contains the class method implementations. The object is an instance of a class that is created when the application is started. An object contains instance variables and pointers to the methods declared within the class.

The class declaration always begins with the @interface compiler directive and ends with the @end directive. Following the class name, separated by a colon, is the name of the parent class. The instance or member variables of the class are declared in a block of code bound by braces ({ and }). List of methods declared by the class follows the instance variable block. A semicolon character marks the end of each instance variable and method declaration.

@interface MyClass : ParentClass
{

//variable declarations

int myInt;

NSString* myString;

}

//method declarations

– (id) initWithString: (NSString*) someString;

+ (void) someMethod;

@end

Methods

Class in Objective-C programming language can have two types of methods: instance methods and class methods. Instance method is a method that is executed by the object, i.e. class instance. In other words, before calling instance methods an instance of a class has to be created first. Unlike instance methods, it is not necessary to create an instance of a class for calling class methods.

The declaration of a method consists of the method type identifier, a return type, one or more signature keywords, and the parameter type and name information.

Method declaration starts with method type identifier – a plus or a minus sign. Minus sign defines an instance method and the plus sign defines a class method. The type of the return variable follows. Method keywords are defined before parameter types and parameter names, as shown below.

– (id) initWithString: (NSString*) someString;