Introduction


Contents

What is 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.

Objective-C includes, when compared to C, a few more keywords and constructs, a short description of which follows. For a comlete example of the application of the constructs, see part 3 of this FAQ.


Syntax

@interface declares a new class. It indicates the name of the class, the name of its superclass, the protocols adhered to ( See Protocols ), the layout of the instance variables (similar to the definition of a struct, but including encapsulation information ( See Encapsulation )) 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. A category is not allowed to redefine a class' existing 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 type checking.

- 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++).

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.

See Sample Code .


Differences with C++

Dynamic vs. Static C++ follows the Simula 67 school of OO programming, where Objective-C follows the Smalltalk school.

In C++ the static type of an object determine whether you can send it a message, in Objective-C the dynamic type determine it. The Simula 67 school is safer, in that more errors are detected at compile time. The Smalltalk school is more flexible, as some valid programs will execute correctly in Smalltalk, where they would be rejected by Simula 67.

Stepstone's Objective-C allows you to chose between the dynamic and static binding, GNU and NeXT do not. ANSI C++ allows you to use dynamic binding, but discourages you from doing so.

In many ways, the difference between C++ and Objective-C is more a question of mindset than technical barriers. Are you willing to offer some flexibility for some safety? Advocates for the Simula 67 school claims that a well designed program doesn't need the extra flexibility (a lie), while advocates for the Smalltalk school claims that the errors are no problem in practice (another lie).

Pragmatic differences with C++ include:


Similarities to Smalltalk

What exactly is it that makes Objective-C have `classes similar to Smalltalk', and what are the resulting capabilities of Objective-C?

Objective-C is as close to Smalltalk as a compiled language allows. The following is a list of the features `taken' from Smalltalk:

Compiling
Messages
Forwarding
Classes

Nice Features


Common problems

What are some of the common problems of the language and how can I work around them?

Multiple inheritance

No class variables

Different return types for methods with the same name If two different classes both happen to have a method of the same name, and the return type of the methods differ, you get a warning from the compiler when it comes to the use of that method in your code, and one of the method types is chosen over the other.

To avoid compiler warnings, you need to either:

So, if you have:
  @interface MyClass : Object
  {
    int  number;
  }

  - (int)number;
  @end
and
@interface OtherClass : Object
{
    double number;
}

- (double)number;
@end

Your method can be either:
- aMethod
{
    id      MyClass = [[MyClass alloc] init];
    id      OtherClass = [[OtherClass alloc] init];
    int     myNumber = [(MyClass *)MyClass number];
    double  otherNumber = [(OtherClass *)OtherClass number];
    return self;
}

or:
- aMethod
{
    MyClass 	*MyClass = [[MyClass alloc] init];
    OtherClass 	*OtherClass = [[OtherClass alloc] init];
    int 	myNumber = [MyClass number];
    double 	otherNumber = [OtherClass number];
    return self;
}


dekorte@symnet.net