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 other chapters in this document. .
Messages
Defined Types
Preprocessor Directives
Compiler Directives
Classes
Categories
Formal Protocols
Method Declarations
Method Implementations
Deprecation Syntax
Naming Conventions
Message expressions are enclosed in square brackets:
[receiver message] |
A variable or expression that evaluates to an object (including the variable self
)
A class name (indicating the class object)
super
(indicating an alternative search for the method implementation)
The message is the name of a method plus any arguments passed to it.
The principal types used in Objective-C are defined in objc/objc.h
. They are:
Type | Definition |
---|---|
An object (a pointer to its data structure). | |
A class object (a pointer to the class data structure). | |
A selector, a compiler-assigned code that identifies a method name. | |
A pointer to a method implementation that returns an | |
A Boolean value, either Note that the type of |
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 |
---|---|
A null object pointer, | |
A null class pointer, | |
A boolean false value, | |
A boolean true value, |
The preprocessor understands these special notations:
Notation | Definition |
---|---|
Imports a header file. This directive is identical to | |
Begins a comment that continues to the end of the line. |
Directives to the compiler begin with “@”. The following directives are used to declare and define classes, categories, and protocols:
Directive | Definition |
---|---|
Begins the declaration of a class or category interface. | |
Begins the definition of a class or category. | |
Begins the declaration of a formal protocol. | |
Ends the declaration/definition of a class, category, or protocol. |
The following mutually exclusive directives specify the visibility of instance variables:
Directive | Definition |
---|---|
Limits the scope of an instance variable to the class that declares it. | |
Limits instance variable scope to declaring and inheriting classes. | |
The default is @protected
.
These directives support exception handling:
Directive | Definition |
---|---|
Defines a block within which exceptions can be thrown. | |
Throws an exception object. | |
Catches an exception thrown within the preceding | |
Defines a block of code that is executed whether exceptions were thrown or not in a preceding |
The following directives support the declared properties feature (see “Declared Properties”):
Directive | Definition |
---|---|
| Begins the declaration of a declared property. |
| Requests that, for the properties whose names follow, the compiler generate accessor methods for which there are no custom implementations. |
| Instructs the compiler not to generate a warning if it cannot find implementations of accessor methods associated with the properties whose names follow. |
In addition, there are directives for these particular purposes:
Directive | Definition |
---|---|
Declares the names of classes defined elsewhere. | |
Returns the compiled selector that identifies method_name. | |
Returns the protocol_name protocol (an instance of the Protocol class). ( | |
Yields a character string that encodes the type structure of type_spec. | |
Defines a constant On Mac OS X v10.4 and earlier, the string must be 7-bit ASCII-encoded. On Mac OS X v10.5 and later (with Xcode 3.0 and later), you can also use UTF-16 encoded strings. (The runtime from Mac OS X v10.2 and later supports UTF-16 encoded strings, so if you use Mac OS X v10.5 to compile an application for Mac OS X v10.2 and later, you can use UTF-16 encoded strings.) | |
Defines a constant | |
Defines a block of code that must be executed only by one thread at a time. |
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 |
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 are declared using the @protocol
directive:
@protocol ProtocolName < protocol list > |
declarations of required methods |
@optional |
declarations of optional methods |
@required |
declarations of required methods |
@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.
The @optional
directive specifies that following methods are optional; the @required
directive directive specifies that following methods must be implemented by a class that adopts the protocol. The default is @required
.
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:
In a protocol declaration, to incorporate other protocols (as shown earlier)
In a class or category declaration, to adopt the protocol (as shown in “Classes” and “Categories”)
In a type specification, to limit the type to objects that conform to the protocol
Within protocol declarations, these type qualifiers support remote messaging:
Type Qualifier | Definition |
---|---|
The method is for asynchronous messages and has no valid return type. | |
The argument passes information to the remote receiver. | |
The argument gets information returned by reference. | |
The argument both passes information and gets information. | |
A copy of the object, not a proxy, should be passed or returned. | |
A reference to the object, not a copy, should be passed or returned. |
The following conventions are used in method declarations:
Argument and return types are declared using the C syntax for type casting.
Arguments are declared after colons (:), for example:
- (void)setWidth:(int)newWidth height:(int)newHeight |
Typically, a label describing the argument precedes the colon—the following example is valid but is considered bad style:
- (void)setWidthAndHeight:(int)newWidth :(int)newHeight |
Both labels and colons are considered part of the method name.
The default return and argument type for methods is id
, not int
as it is for functions. (However, the modifier unsigned
when used without a following type always means unsigned int
.)
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
.
Syntax is provided to mark methods as deprecated:
@interface SomeClass |
-method __attribute__((deprecated)); |
@end |
or:
#include <AvailabilityMacros.h> |
@interface SomeClass |
-method DEPRECATED_ATTRIBUTE; // or some other deployment-target-specific macro |
@end |
This syntax is available only in Objective-C 2.0 and later.
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:
A class can declare methods with the same names as methods in other classes.
A class can declare instance variables with the same names as variables in other classes.
An instance method can have the same name as a class method.
A method can have the same name as an instance variable.
Method names beginning with “_”, a single underscore character, are reserved for use by Apple.
Likewise, protocols and categories of the same class have protected name spaces:
A protocol can have the same name as a class, a category, or anything else.
A category of one class can have the same name as a category of another class.
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.
© 2009 Apple Inc. All Rights Reserved. (Last updated: 2009-05-06)