Join / Forgotten your password?
 
HomeFeaturesStoreForumsWikiWorkshopsJobsPortfolioGalleryEvents Members
 
> CGWiki Home       > Community Portal       > Current Events      > Recent Changes     > Random Page       > Join       > Support Forum       > Help     
 

Objective-C

Objective-C is an object oriented computer programming language. It is a superset of ANSI C and provides classes and message passing similar to Smalltalk.

Contents

Features

Syntax

Objective-C includes, when compared to C, a few more keywords and constructs, a short description of which follows.

`@interface' declares a new class. It indicates the name of the class, the name of its superclass, the protocols adhered to (see Q7), the layout of the instance variables (similar to the definition of a struct, but including encapsulation information (see Q6)) and declares the methods implemented by this class. A class' interface usually resides in a file called `<classname>.h'.

`@implementation' defines a class. The implementation is no more than a collection of method definitions. Without an implementation, a class does not exist at run time. The implementation of a class usually resides in a file called `<classname>.m'.

A category is a named collection of method definitions which are added to an existing class, possibly at run time. Thus, you use a category to add methods to a class. Some Objective-C implementations allow a category to even replace methods.

Objective-C includes the predefined type `id' which stands for a pointer to some object. Thus, `id obj;' declares a pointer to an object. The actual class of the object being pointed to is almost irrelevant, since Objective-C does run-time binding.

`-message;' declares a method called `message'. The `-' indicates that the message can be sent to objects. A `+' instead indicates the message can be sent to class objects. A method is similar to a function in that it has arguments and a return value. The default return type is `id'. If a method has nothing useful to return, it returns `self', which is a pointer to the object to which the message was sent (similar to `this' in C++).

[obj message], [obj message: arg1] and [obj message: arg1 with: arg2] are examples of sending a message to the object OBJ with 0, 1 and 2 arguments respectively. The name of the message is called the selector. In this example, the selectors are: `message', `message:' and `message:with:', respectively.

#import

In the C language, the #include pre-compile directive allows for the insertion of entire files before any compilation actually begins. Objective-C adds the #import directive, which does the same thing, except that it knows not to insert a file that has already been inserted.

For example, if file A includes files X and Y, but X and Y each include the file Q, then Q will be inserted twice into the resultant file, causing "duplicate definition" compile errors. But if file Q is included using the #import directive, only the first inclusion of Q will occur—all others will be ignored.

GCC permits the use of #import in C and C++ as well as Objective-C. However, it is not in standard C or C++ and should therefore not be used by portable programs.

External Links

Wikipedia: Objective-C
Introduction to the Objective-C Programming Language (developer.apple.com)