Retired Document
Important: You should read Object-Oriented Programming with Objective-C and The Objective-C Programming Language instead.
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] |
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.
Defined Types
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 |
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:
Preprocessor Directives
Compiler Directives
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 |
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. | |
Yields the internal data structure of class_name instances | |
Defines a constant | |
Defines a constant | |
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:
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. |
Method Declarations
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
, notint
as it is for functions. (However, the modifierunsigned
when used without a following type always meansunsigned int
.)
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.
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:
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.
Copyright © 2008 Apple Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2008-06-09