Objective-C is a programming language object-oriented reflexive . This is an extension of ANSI C , such as C ++ , but differs from it by its dynamic message delivery, his typing weak or strong, its dynamic typing and dynamic loading. Unlike C ++, it does not allow multiple inheritance, but there are ways to combine the advantages of C ++ and Objective-C.

Today, it is mainly used in Apple’s operating systems : Mac OS X and its iOS derivative, based on the Cocoa class library but there is also a free GNUstep class library under GNU / Linux . Cocoa and GNUstep are the successors of the OpenStep API , used in the NeXTSTEP and OPENSTEP operating systems .


The late 1970s was marked by the rapid popularity of the C language invented earlier in the decade by Dennis Ritchie at AT & T Bell Laboratories . This popularity is maintained by another still greater, that of the operating system UNIX completely written in C 1 . At the turn of the decades 1970- 1980 , the common practice of software engineering is based on structured programming . The implementation of structured programming is used to split large programs into smaller, less complex, and therefore easier to program, parts. However, As problem solving becomes increasingly complex and complex, structured programming becomes less useful as more and more procedures are to be written, leading to complex and weak control structures Reuse of code. At the time, many see the object-oriented as a possible solution to this problem.

Several people then create extensions to the C to add the object-oriented. This is the case of Bjarne Stroustrup who develops C ++ in 1979 in the same laboratories as those in which Dennis Ritchie’s C was born. The birth of Objective-C comes in the same context. Brad Cox ‘s being developed in the early 1980. The language is based on another, the Smalltalk-80 , and is intended to be an additional layer C to allow the creation and manipulation of objects 2 .

The code compiled Objective-C is running in an execution environment ( runtime ) Light written in C, which adds little to the size of the application.

The first operating system to use Objective-C was NeXTSTEP , from NeXT , founded by Steve Jobs .

Objective-C is widely used on Macintosh , especially for API Cocoa for Mac OS X and more recently for the development of applications iPhone , the smartphone from Apple.

There is also a free implementation of the OpenStep framework , called GNUstep , which is multiplatform and runs on GNU / Linux , Microsoft Windows and most UNIX .

Language description

In Objective-C, everything is an object just as in Smalltalk from which it draws heavily. It is therefore a language strongly object oriented. Simple inheritance induces an inheritance tree with a root: the NSObject class, in the case of Cocoa / NeXTSTEP, or Object in the case of GNUstep. It is from this that all classes will derive. For example, an object of class NSString, or NSArray, derives from the NSObject class (indirectly). NSMutableArray is derived from the NSArray class, which is its superclass. According to some of its users, this is where the power of Objective-C appears: unlike C ++ or other languages ​​that do not include it, weak typing makes it easier to manipulate data.

Indeed, rather than having to manipulate many types, there are only a few, for example in the case of Cocoa:

  • Id that is a pointer to any object type .
  • BOOL that is the same as char of C, but used as a Boolean value. 0 is considered false, and all that is not 0 is considered true (A, a, 1, 2, etc.)
  • YES which is worth 1.
  • NO which is 0.
  • IBOutlet is a macro that only tells Interface Builder that it is in front of an “Outlet”.
  • IBAction which is equivalent to void, and tells Interface Builder that it is an action (or class method) to be called, for example, by a pressed button.
  • Nil which is identical to NULL. It will be preferred to this one because a “message” sent to nil does not create an exception, and therefore a program crash, but does not return “nothing”, therefore “nil”.

In addition, all class instance variables are protected by default, and public class methods. It allows programmers to have a more rigorous programming, while being faster, and respecting the concepts of the OOP.

Another aspect is the KVC (Key-Value Coding) design model, also inspired by Smalltalk , which defines the access to a variable by its name. For example, in the case of a Person class, with an instance variable Surname, of type NSString:

@interface Person : NSObject
 // instance variables
 NSString * surname ;
// methods
@property ( copy ) NSString * surname ;

Objective-C allows the fast creation of NSString objects with “@”, in the same way as C with strings “”. NSString is encoded in unicode , that is, unlike C, characters are not limited to ASCII codes. We can therefore set the value of the surname variable of a Person instance in this way:

// We create a pointer to an object of the Person class, which was first allocated in the memory space, and initialized.
Person * example = [[ Person alloc ] init ];
// We send a message to the example pointer that will redirect it to the object to which it points, to fix the value of the surname key.
[ Example setValue : @ "Paul" forKey : @ "surname" ];


In Objective-C, any method call of a class is a message pass. To call a message on an object, place the object and then the message between the bracket. Calls can be chained very easily. Thus in the example, method returns an object and on this object we call method2 .

// message passing
[ method object : argument ]; // message chaining [[ method object : argument ] method2 : argument2 ];

The syntax of the methods, a bit confusing at first, was thought to be more like human language. Each argument is separated by “:” as well as a comment.

// declaration of a method
- ( void ) eatWith: ( NSString * ) name andSpeakAbout: ( NSString * ) subject ;
// example call method on an object
[ Paul eatWith : @ "Pauline" andSpeakAbout : theSubject ];

Each object instance has an isa pointer, that is, a pointer to a metaclass object that describes the methods accessible by the object. A class with a parent, metaclass represents a tree with all the hierarchy of classes currently in memory. So when trying to pass a message, the Objective-C environment retrieves the isa pointer from the object, then traverses the metaclass tree to get the pointer of the method called. This mechanism is obviously expensive, but a caching device at the first call makes it very efficient.

// define the id type that contains the pointer isa
typedef struct objc_class * Class ;
typedef struct objc_object {
 Class isa ;
} * Id ;

Variants of language

Objective-C ++

Objective-C ++ is a front end for GCC , which compiles a combination of code C ++ and Objective-C. Objective-C ++ in addition to C ++ extensions that the Objective-C language adds C .

Notes and references

  1. ↑ ( in ) James Duncan Davidson & Apple Computer, Learning Cocoa with Objective-C , O’reilly,, 382 p. ( ISBN  9780596003012 ) , p.  1
  2. ↑ James Duncan Davidson, op. Cit.