Objective-C is an object-oriented programming language used by Apple primarily for programming Mac OS X, iPhone, and other iOS applications. It is an extension of the C Programming Language.
Objective-C consists of two types of files:
.h: These types of files are called 'Header' or 'Interface files'
.m: These types of files are those which contain your program code logic and make use of the 'Header' files. These are also referred to as 'implementation' files.
Most Object-Oriented development environments consist of the following parts:
An Object-Oriented programming language
An extensive library consisting of objects
A development suite of developer tools
A runtime environment
Directives
In Objective-C, we use the #import directive. If you observe the content of the MyClass.h file, you will notice that at the top of the file is a #import statement:
#import
@Interface myClass : NSObject{
}
@end
The #import statement is known as a "pre-processor directive."
To import a header file from one of the framework libraries, you would specify the header filename using the angle brackets (< >), within the #import statement. If you wanted to import one of your own header files to be used within your project, you would specify and make use of the double quote marks (" "), as you can see from our code file, MyClass.m:
#import "MyClass.h"
@implementation MyClass
@end
Objective-C classes
A Class can simply be defined as a representation of a type of object; think of it as a blueprint that describes the object. Just as a single blueprint can be used to build multiple versions of a car engine, a class can be used to create multiple copies of an object. In Objective-C, you will spend most of your time dealing with classes and class objects. An example of a class object is the NSObject class. NSObject is the root class of most of the Objective-C classes. It defines the basic interface of a class and contains methods that are common to all classes that inherit from it.
The @interface directive
To declare a class, you use the @interface compiler directive that is declared within MyClass.h, as follows:
@interface MyClass : NSObject {
}
The @implementation directive
To implement a class declared within a header file, you use the @implementation compiler directive, as follows:
#import "MyClass.h"
@implementation MyClass
@end
Class instantiation
In Objective-C, in order for us to create an instance of a class, you would typically use the alloc keyword to allocate memory for the object and then return the variable in a class type. This is shown in the following example:
MyClass *myClass = [MyClass alloc];
Class access privileges
In OOP, when you are defining your classes, bear in mind that by default, the access privilege of all fields within a class are @protected. These fields can also be defined as @public,or @private.
The following table below shows the various access privileges that a class can contain:
@private:A class member is only visible to the class that declares it.
@public:A class member is made visible to all classes that instantiate this class.
@protected:Class members are made visible to the class that declares it as well as other classes which inherit from the base class.
0 comments:
Post a Comment