Language Summary

Objective-C adds a small number of constructs to the C language and defines a handful of conventions for effectively interacting with the runtime system. This appendix lists all the additions to the language but doesn’t go into great detail. For more information, see The Language. For a more formal presentation of Objective-C syntax, see Grammar.

Messages

Message expressions are enclosed in square brackets:

[receiver message]

The receiver can be:

The message is the name of a method plus any arguments passed to it.

Defined Types

The principal types used in Objective-C are defined in objc/objc.h. They are:

Type

Definition

id

An object (a pointer to its data structure).

Class

A class object (a pointer to the class data structure).

SEL

A selector, a compiler-assigned code that identifies a method name.

IMP

A pointer to a method implementation that returns an id.

BOOL

A Boolean value, either YES or NO.

id can be used to type any kind of object, class, or instance. In addition, class names can be used as type names to statically type instances of a class. A statically typed instance is declared to be a pointer to its class or to any class it inherits from.

The objc.h header file also defines these useful terms:

Type

Definition

nil

A null object pointer, (id)0.

Nil

A null class pointer, (Class)0.

NO

A boolean false value, (BOOL)0.

YES

A boolean true value, (BOOL)1.

Preprocessor Directives

The preprocessor understands these special notations:

Notation

Definition

#import

Imports a header file. This directive is identical to #include, except that it doesn’t include the same file more than once.

//

Begins a comment that continues to the end of the line.

Compiler Directives

Directives to the compiler begin with “@”. The following directives are used to declare and define classes, categories, and protocols:

Directive

Definition

@interface

Begins the declaration of a class or category interface.

@implementation

Begins the definition of a class or category.

@protocol

Begins the declaration of a formal protocol.

@end

Ends the declaration/definition of a class, category, or protocol.

The following mutually exclusive directives specify the visibility of instance variables:

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.

The default is @protected.

These directives support exception handling:

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

Defines a block of code that is executed whether exceptions were thrown or not in a preceding @try block.

In addition, there are directives for these particular purposes:

Directive

Definition

@class

Declares the names of classes defined elsewhere.

@selector(method_name)

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

@encode(type_spec)

Yields a character string that encodes the type structure of type_spec.

@defs(class_name)

Yields the internal data structure of class_name instances

@"string"

Defines a constant NSString object in the current module and initializes the object with the specified 7-bit ASCII-encoded string.

@"string1" @"string2" ... @"stringN"

Defines a constant NSString object in the current module. The string created is the result of concatenating the strings specified in the two directives.

@synchronized()

Defines a block of code that must be executed only by one thread at a time.

Classes

A new class is declared with the @interface directive. The interface file for its superclass must be imported:

#import "ItsSuperclass.h"
 
@interface ClassName : ItsSuperclass < protocol_list >
{
    instance variable declarations
}
method declarations
@end

Everything but the compiler directives and class name is optional. If the colon and superclass name are omitted, the class is declared to be a new root class. If any protocols are listed, the header files where they’re declared must also be imported.

A file containing a class definition imports its own interface:

#import “ClassName.h”
 
@implementation ClassName
method definitions
@end

Categories

A category is declared in much the same way as a class. The interface file that declares the class must be imported:

#import "ClassName.h"
 
@interface ClassName ( CategoryName ) < protocol list >
method declarations
@end

The protocol list and method declarations are optional. If any protocols are listed, the header files where they’re declared must also be imported.

Like a class definition, a file containing a category definition imports its own interface:

#import "CategoryName.h"
 
@implementation ClassName ( CategoryName )
method definitions
@end

Formal Protocols

Formal protocols are declared using the @protocol directive:

@protocol ProtocolName < protocol list >
method declarations
@end

The list of incorporated protocols and the method declarations are optional. The protocol must import the header files that declare any protocols it incorporates.

You can create a forward reference to a protocol using the @protocol directive in the following manner:

@protocol ProtocolName;

Within source code, protocols are referred to using the similar @protocol() directive, where the parentheses enclose the protocol name.

Protocol names listed within angle brackets (<...>) are used to do three different things:

Within protocol declarations, these type qualifiers support remote messaging:

Type Qualifier

Definition

oneway

The method is for asynchronous messages and has no valid return type.

in

The argument passes information to the remote receiver.

out

The argument gets information returned by reference.

inout

The argument both passes information and gets information.

bycopy

A copy of the object, not a proxy, should be passed or returned.

byref

A reference to the object, not a copy, should be passed or returned.

Method Declarations

The following conventions are used in method declarations:

Method Implementations

Each method implementation is passed two hidden arguments:

Within the implementation, both self and super refer to the receiving object. super replaces self as the receiver of a message to indicate that only methods inherited by the implementation should be performed in response to the message.

Methods with no other valid return typically return void.

Naming Conventions

The names of files that contain Objective-C source code have the .m extension. Files that declare class and category interfaces or that declare protocols have the .h extension typical of header files.

Class, category, and protocol names generally begin with an uppercase letter; the names of methods and instance variables typically begin with a lowercase letter. The names of variables that hold instances usually also begin with lowercase letters.

In Objective-C, identical names that serve different purposes don’t clash. Within a class, names can be freely assigned:

Likewise, protocols and categories of the same class have protected name spaces:

However, class names are in the same name space as global variables and defined types. A program can’t have a global variable with the same name as a class.