| I l@ve RuBoard |
|
1.4 Compiler and Preprocessor DirectivesCompiler and preprocessor directives are special terms in your program that tell the compiler to perform some Objective-C-specific action. All compiler directives start with the character @, and preprocessor directives start with #. 1.4.1 Class Declarations and DefinitionsThe following compiler directives are used to begin or conclude the declaration and definition of Objective-C classes, categories, and protocols:
The use of these compiler directives is described in detail under Section 1.3, and especially in the subsections Section 1.3.2, Section 1.3.6, and Section 1.3.7. 1.4.2 Forward DeclarationsThe following compiler directives generate forward declarations, informing the compiler that a type exists:
Use forward declarations when an interface uses variables (fields or method parameters or return types) of a given type. Rather than import the header file for that type, you can simply forward reference it. For example: @class Point ;
@interface Circle : Graphic {
Point * center ;
// etc.
}
@end
This is sufficient because the declaration of Circle does not use any details of Point. You could import the header for Point, but this makes any other class that imports Circle.h appear dependent also on Point.h, and can cause unnecessary (and slow) recompiling in large projects. 1.4.3 Expanding DirectivesExpanding complier directives look like functions, but they are really instructions to the compiler, which expands them as described in the following list:
The directives @encode, @defs, and @"string" deserve some additional explanation. 1.4.3.1 Using @encodeThe following example shows how @encode can be used to get the string that the Objective-C runtime uses to describe a type: char * itype = @encode (int ); The result of this statement will be to define itype as the one-character string "i", which is the runtime representation of an int type. The @encode directive can take any C or Objective-C type. The runtime uses the mapping between types and strings to encode the signatures of methods and associate them with selectors. You can use @encode to implement your own object storage and retrieval, or other tasks that need to describe the types of values. Table 1-1 shows the results of applying @encode to C and Objective-C types.
The runtime system also uses encodings for type qualifiers, shown in Table 1-2, and you may encounter them in its representation of method signatures. However, you can't get those encodings with the @encode directive.
1.4.3.2 Using @defsThe following example shows how @defs can be used to create a C structure with fields that match the fields in a class. In this example, both the order and type of the fields of MyStruct will match those in MyClass: @interface MyClass : Object {
int i ;
}
@end
typedef struct {
@defs (MyClass )
} MyStruct ;
The typedef in this example is seen by the compiler as: typedef struct {
id isa;
int i ;
} MyStruct ;
Having a structure that corresponds to a class lets you bypass the normal access restrictions on an Objective-C instance. In the following example, an instance of MyClass is cast to an instance of MyStruct. Once that's done, the protected field i can be accessed with impunity: MyClass* c = [MyClass new]; MyStruct* s = (MyStruct*)c; s->i = 42; Obviously this is a facility you should use with restraint. 1.4.3.3 Using @"string"The @"string" directive creates the Objective-C version of a string literal: one that you can pass to a method expecting a string object, or use to initialize or compare with another string object. When you create a string with @"string", you get an instance of a class defined by the compiler option -fconstant-string-class. The instance is static: its contents are stored in your program's file, and the instance is created at runtime and kept around for the duration of program execution. You can't change its value, because it appears as an expression in your program, not as a variable In the GNU runtime, the default string class is NXConstantString; for Darwin it is the Cocoa class NSConstantString. 1.4.4 Preprocessor SymbolsObjective-C adds one preprocessor directive and defines one symbol to the preprocessor before compiling:
|
| I l@ve RuBoard |
|