Objective-C keywords

Here in this section we will know about the keywords used in objective-C language.

Objective-C keywords


Here in this section we will know about the keywords used in objective-C language. Objective-C is a superset of C language, so program written in c and C++ should compile as objective-c. It provides some additional keywords, to avoid conflict with keywords in other language it uses [email protected]? at the beginning of keyword. These keyword are called Compiler Directives.


Directives used to declare and define classes, categories and protocols:

Directive Definition
@interface used to declare of class or interface.
@implementation used to define a class or category.
@protocol used to declare a formal protocol.
@end ends the declaration, definition, category or protocol.

Directive used to specify the visibility of the instance. Default is @protected.

Directive Definition
@private Limits the scope of an instance variable to the class that declares it.
@protected Limits instance variable scope to declaring and inheriting classes.
@public Removes restrictions on the scope of instance variables.

Exception handling directives.

Directive Definition
@try Defines a block within which exceptions can be thrown.
@throw Throws an exception object.
@catch Catches an exception thrown within the preceding @try block.
@finally A block of code that is executed whether exceptions were thrown or not in a @try block.

Directive used for particular purpose.

Directive Definition
@class Declares the names of classes defined elsewhere.
@selector(method_name) It returns the compiled selector that identifies method_name.
@protocol(protocol_name) Returns the protocol_name protocol (an instance of the Protocol class). (@protocol is also valid without (protocol_name) for forward
@encode(type_spec) Yields a character string that encodes the type structure of type_spec.
@"string" Defines a constant NSString object in the current module and
initializes the object with the specified 7-bit ASCII-encoded string.
@"string1" @"string2" ...
Defines a constant NSString object in the currentmodule. The string
created is the result of concatenating the strings specified in the two
@synchronized() Defines a block of code that must be executed only by one thread
at a time.

Some keywords of Objective-C are not reserved outside. These are?..

in out inout bycopy
byref oneway    

Keyword for memory management in Objective-C 
These are looking as keywords but infact these are methods of root class NSObject.

alloc retain release autorelease

Some other keywords:

1.  bool
is a keyword used in objective-C but its value is here YES or NO. In C and C++ it has value either TRUE or FALSE.
2. 'super' and 'self' can be treated as keywords but self is a hidden parameter to each method and super gives the instructions to the compiler that how to use self differently.

Preprocessor Directives
The preprocessor directives are special notations:

Directive Definition
// This is used to comment a single line.
#import  Like C and C++ it is used to include a file but it doesn't include more than once.