Cover | Table of Contents
Book object might have fields to
specify the author and publisher. In addition, objects interact
through message passing, which provides an alternative to the function
calls of a procedural language.MyClass is said to be an
instance of MyClass.@, and preprocessor directives start with
#.@interface @protocol @implementation @end @class
ClassName@protocol
ProtocolName@class Point; @interface Circle : Graphic { Point* center; // etc. } @end
@encode ( typeName
)-fconstant-string-class=
ClassName@"string"
directive. For the GNU runtime, the default class is NXConstantString; for Cocoa it is
NSConstantString.-fgnu-runtime-fnext-runtime-gen-decls-Wno-protocol-Wselectorout and in
modify the parameters in the following declaration:-(void)getData:(out Data*)data forIndex:(in int)index;
in
out Object
and NSObject
provide the following methods for managing the lifecycles
of objects:+initialize+alloc+new-init-copy-deallocObject also
provides these methods:-shallowCopy-deepCopy-deepen-free1Circle* c = [Circle alloc]; 2 c = [c init];
Object root class
provides simple error-handling capability; the Cocoa framework
implements exception raising and handling.-error : method on the object whose method generated the
error. You can override this method to customize error handling
for a particular class.-error: method
prepares information about the receiver and passes it to the
runtime C function objc_verror(
) .objc_verror( )
function calls the runtime error handler function if there is one;
otherwise it writes an error message to stderr. You can provide a handler
function to customize error handling for all classes.abort( ) and exits.objc_error_handler objc_set_error_handler(objc_error_handlerf)typedef BOOL (*objc_error_handler) (id receiver, int errCode, const char* format, va_list args);
receivererrCodeobjc_error_handler .formatprintf-style C
string, for printing an error message.args+alloc message to create other objects.-class method. For example, given an instance created this
way:Circle* c = [Circle new];
ClasscircleClass = [c class];
+alloc.isa field, pointing to their class object. The class object
contains (or refers to) the instance methods that the object can
respond to. In addition each class object has a Object. This section describes the Object supplied with the GNU runtime. The
Object class provided by Darwin is
similar, but Darwin users will be more likely to use the NSObject root class that comes with the
Cocoa class library. (The GNUstep library also provides an NSObject root class almost identical to
Cocoa's.) This section describes the fields and methods of both Object and NSObject.Object and NSObject declare
only one field, isa , whose type is Class. This field is inherited by all their
descendants, which is critical to the operation of inheritance and
message dispatch. It points to the class object representing the
instance's class. See Section
1.9 earlier in this book.-forward :: method, the runtime calls this method, passing it
information about the unhandled call. The return value from the
forwarded call is propagated back to the original caller of the
method.Object and NSObject provide a -forward:: method, so all objects you are
likely to encounter will behave as described in this section. In each
case, the default behavior, implemented by the root class method, is to
exit the program when an unhandled call is forwarded. Your classes can
override this method to provide alternative behavior; common choices are
to absorb the error or to redirect (forward) the unhandled message to
another object, which is called a delegate
.-forward:: method takes a selector and a second parameter that
stores information about the parameters passed to the original
(failed) method call.Object sets off
the following train of events:-forward:: calls -doesNotRecognize : on self,
passing the selector for the original method call.-doesNotRecognize: in
turn calls -error : passing a format string and the name of the
original method call.-error: logs an error
message and aborts the program, unless you have overridden this
method or installed an alternative error handler. Section 1.8 discusses this
more.-forward::
to return NULL. To implement
true forwarding for a class, override the -forward:: method in the following
way:1 -(retval_t)forward:(SEL)sel :(arglist_t)args { 2 if ([myDelegate respondsTo:sel]) 3 return [myDelegate performv:sel :args] 4 else 5 return [super forward:sel :args]; 6 }
-free method (if it inherits from Object) or its -dealloc method (if it inherits from NSObject). It will be your responsibility to
ensure that your program no longer needs the object.Object, you can use its methods -write : and -read
:, along with some functions provided by the runtime and
a helper class called TypedStream
.@interface MyClass : Object { AnotherClass* obj; int i; } ... @end
1 -(id)write:(TypedStream*)stream { 2 [super write:stream]; 3 objc_write_types(stream, "@i", obj, &i); 4 return self; 5 }
-write:.objc_write_types( ) . The second parameter is a concatenation of descriptors
for the types of fields you are writing. These are one-character
strings, the same as those used by the @encode directive. They are listed in
objc-api.h.1 -(id)read:(TypedStream*)stream { 2 [super read:stream]; 3 objc_read_types(stream, "@i", obj, &i); 4 return self; 5 }
-read:.objc_read_types( ) . The second parameter is a concatenation of descriptors
for the types of the fields you are reading. These are one-character
strings, the same as those used by the parent field of a window
object:Window*parentWind = [wind valueForKey:@"parent"];
-valueForKey : as well as a literal value, this is another way your
program's behavior can vary based on values that aren't known until
runtime.NSObject implements -valueForKey: method as part of the NSKeyValueCoding category, which declares methods for reading from and
writing to the fields of objects. These methods store and retrieve
Objective-C objects, so their primary use is in accessing objects.
However, even if your fields are integers or other numeric types, you
can still use key-value coding to retrieve and set them. The methods
will take NSNumber objects and
automatically convert them to set numeric fields, and return NSNumber objects when you read numeric
fields.+accessInstanceVariablesDirectly to return NO.field will look for
accessor methods with the following names (the order will vary as
described in the next section):getFieldfield_getField_fieldget or [obj methodName:anInt];
1SEL sel = @selector(methodName:); 2 typedef void (*MpType) (id, SEL, int); 3 MpType mptr = (MpType) [obj methodFor:sel]; 4 ... 5 mptr(obj, sel, anInt);
mptr takes these into account, as well as the
return (void) and parameter
(int) types of the method.-methodFor : (for descendants of Object) or -methodForSelector : (for descendants of NSObject). If you don't have the receiver or
an object of the same type, you can call the class methods id
as a C++ template name. If you could, the declaration id< TypeName
>
var could be either a C++ template
declaration or an Objective-C declaration using a protocol.[obj methodName: ::aGlobal].
Return to Objective-C Pocket Reference