2
<title>NSObject</title>
5
<a href ="NSNumberFormatter.html">[Previous] </a>
6
<a href ="Base.html">[Up] </a>
7
<a href ="NSPipe.html">[Next] </a>
11
<dt><a href ="http://www.gnustep.org/developers/whoiswho.html">Richard Frith-Macdonald</a>
13
<dt><a href ="http://www.gnustep.org/developers/whoiswho.html">Nicola Pero</a>
16
<p>Version: $Revision: 1.16 $</p>
17
<p>Date: $Date: 2001/12/12 14:10:13 $</p>
18
<h2><a name ="cont-0">NSObject</a></h2>
19
<h2><a name ="NSObject">NSObject</a></h2>
20
<p><b>Declared in: </b> Foundation/NSObject.h</p>
21
<p><b>Conforms to: </b> NSObject
27
<code>NSObject</code> is the root class (a root class is
28
a class with no superclass) of the gnustep base library
29
class hierarchy, so all classes normally inherit from
30
<code>NSObject</code>. There is an exception though:
31
<code>NSProxy</code> (which is used for remote messaging)
32
does not inherit from <code>NSObject</code>.
37
Unless you are really sure of what you are doing, all
38
your own classes should inherit (directly or indirectly)
39
from <code>NSObject</code> (or in special cases from
40
<code>NSProxy</code>). <code>NSObject</code> provides
41
the basic common functionality shared by all gnustep
47
The essential methods which must be implemented by all
48
classes for their instances to be usable within gnustep
49
are declared in a separate protocol, which is the
50
<code>NSObject</code> protocol. Both
51
<code>NSObject</code> and <code>NSProxy</code> conform to
52
this protocol, which means all objects in a gnustep
53
application will conform to this protocol (btw, if you
54
don't find a method of <code>NSObject</code> you are
55
looking for in this documentation, make sure you also
56
look into the documentation for the <code>NSObject</code>
62
Theoretically, in special cases you might need to
63
implement a new root class. If you do, you need to make
64
sure that your root class conforms (at least) to the
65
<code>NSObject</code> protocol, otherwise it will not
66
interact correctly with the gnustep framework. Said
67
that, I must note that I have never seen a case in which
68
a new root class is needed.
73
<code>NSObject</code> is a root class, which implies that
74
instance methods of <code>NSObject</code> are treated in
75
a special way by the Objective-C runtime. This is an
76
exception to the normal way messaging works with class
77
and instance methods: if the Objective-C runtime can't
78
find a class method for a class object, as a last resort
79
it looks for an instance method of the root class with
80
the same name, and executes it if it finds it. This
81
means that instance methods of the root class (such as
82
<code>NSObject</code>) can be performed by class objects
83
which inherit from that root class ! This can only
84
happen if the class doesn't have a class method with the
85
same name, otherwise that method - of course - takes the
86
precedence. Because of this exception,
87
<code>NSObject</code>'s instance methods are written in
88
such a way that they work both on <code>NSObject</code>'s
89
instances and on class objects.
93
<h2>Instance Variables </h2>
98
<li ><a href ="NSObject.html#method-0">+alloc</a>
99
<li ><a href ="NSObject.html#method-1">+allocWithZone:</a>
100
<li ><a href ="NSObject.html#method-2">+cancelPreviousPerformRequestsWithTarget:selector:</a>
101
<li ><a href ="NSObject.html#method-3">+class</a>
102
<li ><a href ="NSObject.html#method-4">+conformsToProtocol:</a>
103
<li ><a href ="NSObject.html#method-5">+copyWithZone:</a>
104
<li ><a href ="NSObject.html#method-6">+description</a>
105
<li ><a href ="NSObject.html#method-7">+descriptionWithLocale:</a>
106
<li ><a href ="NSObject.html#method-8">+descriptionWithLocale:indent:</a>
107
<li ><a href ="NSObject.html#method-9">+descriptionWithLocale:indent:to:</a>
108
<li ><a href ="NSObject.html#method-10">+initialize</a>
109
<li ><a href ="NSObject.html#method-11">+instanceMethodForSelector:</a>
110
<li ><a href ="NSObject.html#method-12">+instanceMethodSignatureForSelector:</a>
111
<li ><a href ="NSObject.html#method-13">+instancesRespondToSelector:</a>
112
<li ><a href ="NSObject.html#method-14">+load</a>
113
<li ><a href ="NSObject.html#method-15">+mutableCopyWithZone:</a>
114
<li ><a href ="NSObject.html#method-16">+new</a>
115
<li ><a href ="NSObject.html#method-17">+poseAsClass:</a>
116
<li ><a href ="NSObject.html#method-18">+setVersion:</a>
117
<li ><a href ="NSObject.html#method-19">+superclass</a>
118
<li ><a href ="NSObject.html#method-20">+version</a>
119
<li ><a href ="NSObject.html#method-47">-_dealloc</a>
120
<li ><a href ="NSObject.html#method-21">-awakeAfterUsingCoder:</a>
121
<li ><a href ="NSObject.html#method-22">-classForArchiver</a>
122
<li ><a href ="NSObject.html#method-23">-classForCoder</a>
123
<li ><a href ="NSObject.html#method-24">-classForPortCoder</a>
124
<li ><a href ="NSObject.html#method-25">-connection:handleRequest:</a>
125
<li ><a href ="NSObject.html#method-26">-copy</a>
126
<li ><a href ="NSObject.html#method-27">-dealloc</a>
127
<li ><a href ="NSObject.html#method-45">-deallocNotificationsActive</a>
128
<li ><a href ="NSObject.html#method-28">-description</a>
129
<li ><a href ="NSObject.html#method-29">-descriptionWithLocale:</a>
130
<li ><a href ="NSObject.html#method-30">-descriptionWithLocale:indent:</a>
131
<li ><a href ="NSObject.html#method-31">-descriptionWithLocale:indent:to:</a>
132
<li ><a href ="NSObject.html#method-32">-doesNotRecognizeSelector:</a>
133
<li ><a href ="NSObject.html#method-33">-forwardInvocation:</a>
134
<li ><a href ="NSObject.html#method-34">-hash</a>
135
<li ><a href ="NSObject.html#method-35">-init</a>
136
<li ><a href ="NSObject.html#method-36">-isEqual:</a>
137
<li ><a href ="NSObject.html#method-37">-methodForSelector:</a>
138
<li ><a href ="NSObject.html#method-38">-methodSignatureForSelector:</a>
139
<li ><a href ="NSObject.html#method-39">-mutableCopy</a>
140
<li ><a href ="NSObject.html#method-40">-performSelector:withObject:</a>
141
<li ><a href ="NSObject.html#method-41">-performSelector:withObject:afterDelay:inModes:</a>
142
<li ><a href ="NSObject.html#method-42">-replacementObjectForArchiver:</a>
143
<li ><a href ="NSObject.html#method-43">-replacementObjectForCoder:</a>
144
<li ><a href ="NSObject.html#method-44">-replacementObjectForPortCoder:</a>
145
<li ><a href ="NSObject.html#method-46">-setDeallocNotificationsActive:</a>
147
<hr><h2>Class Methods </h2>
148
<h3><a name ="method-0">alloc</a></h3>
149
+ (id) <b>alloc</b>;<br>
151
Allocates a new instance of the receiver from the default
152
zone, by invoking <code>allocWithZone:</code> with
153
<code>NSDefaultMallocZone()</code> as the zone argument.
154
Returns the created instance.
157
<h3><a name ="method-1">allocWithZone:</a></h3>
158
+ (id) <b>allocWithZone:</b> (NSZone*)zone;<br>
160
This is the basic method to create a new instance. It
161
allocates a new instance of the receiver from the specified
165
Memory for an instance of the receiver is allocated; a
166
pointer to this newly created instance is returned. All
167
instance variables are set to 0 except the
168
<code>isa</code> pointer which is set to point to the
169
object class. No initialization of the instance is
170
performed: it is your responsibility to initialize the
171
instance by calling an appropriate <code>init</code>
172
method. If you are not using the garbage collector, it is
173
also your responsibility to make sure the returned
174
instance is destroyed when you finish using it, by calling
175
the <code>release</code> method to destroy the instance
176
directly, or by using <code>autorelease</code> and
182
You do not normally need to override this method in
183
subclasses, unless you are implementing a class which for
184
some reasons silently allocates instances of another class
185
(this is typically needed to implement class clusters and
186
similar design schemes).
191
If you have turned on debugging of object allocation (by
192
calling the <code>GSDebugAllocationActive</code>
193
function), this method will also update the various
194
debugging counts and monitors of allocated objects, which
195
you can access using the <code>GSDebugAllocation...</code>
201
<h3><a name ="method-2">cancelPreviousPerformRequestsWithTarget:selector:</a></h3>
202
+ (void) <b>cancelPreviousPerformRequestsWithTarget:</b> (id)aTarget <b>selector:</b> (SEL)aSelector;<br>
206
<h3><a name ="method-3">class</a></h3>
207
+ (Class) <b>class</b>;<br>
209
Returns the receiver.
212
<h3><a name ="method-4">conformsToProtocol:</a></h3>
213
+ (BOOL) <b>conformsToProtocol:</b> (Protocol*)aProtocol;<br>
217
<h3><a name ="method-5">copyWithZone:</a></h3>
218
+ (id) <b>copyWithZone:</b> (NSZone*)zone;<br>
222
<h3><a name ="method-6">description</a></h3>
223
+ (NSString*) <b>description</b>;<br>
225
The <code>description</code> factory method describes the class
226
of the receiver. In the default GNUstep implementation, this
227
simply returns the name of the class as an NSString object.
230
<h3><a name ="method-7">descriptionWithLocale:</a></h3>
231
+ (NSString*) <b>descriptionWithLocale:</b> (NSDictionary*)aLocale;<br>
232
Standards: GNUstep<br>
234
The default (NSOBject) implementation of this method simply calls
235
the <code>description</code> method and discards the locale
239
<h3><a name ="method-8">descriptionWithLocale:indent:</a></h3>
240
+ (NSString*) <b>descriptionWithLocale:</b> (NSDictionary*)aLocale <b>indent:</b> (unsigned int)level;<br>
241
Standards: GNUstep<br>
243
The default (NSObject) implementation of this method simply calls
244
the <code>descriptionWithLocale:</code> method and discards the
248
<h3><a name ="method-9">descriptionWithLocale:indent:to:</a></h3>
249
+ (NSString*) <b>descriptionWithLocale:</b> (NSDictionary*)aLocale <b>indent:</b> (unsigned int)level <b>to:</b> (id<GNUDescriptionDestination>)output;<br>
250
Standards: GNUstep<br>
252
The default (NSObject) implementation of this method simply calls
253
the <code>descriptionWithLocale:indent:</code> method and appends
254
the value returned by that method to the output object.
257
<h3><a name ="method-10">initialize</a></h3>
258
+ (void) <b>initialize</b>;<br>
262
<h3><a name ="method-11">instanceMethodForSelector:</a></h3>
263
+ (IMP) <b>instanceMethodForSelector:</b> (SEL)aSelector;<br>
267
<h3><a name ="method-12">instanceMethodSignatureForSelector:</a></h3>
268
+ (NSMethodSignature*) <b>instanceMethodSignatureForSelector:</b> (SEL)aSelector;<br>
270
Returns a method signature object representing the
271
method implemented for instsances of this class which
272
corresponds to the supplied selector.
275
<h3><a name ="method-13">instancesRespondToSelector:</a></h3>
276
+ (BOOL) <b>instancesRespondToSelector:</b> (SEL)aSelector;<br>
280
<h3><a name ="method-14">load</a></h3>
281
+ (void) <b>load</b>;<br>
285
<h3><a name ="method-15">mutableCopyWithZone:</a></h3>
286
+ (id) <b>mutableCopyWithZone:</b> (NSZone*)zone;<br>
290
<h3><a name ="method-16">new</a></h3>
291
+ (id) <b>new</b>;<br>
293
This method is a short-hand for alloc followed by init, that is,
296
NSObject *object = [NSObject new];
299
is exactly the same as
302
NSObject *object = [[NSObject alloc] init];
307
This is a general convention: all <code>new...</code>
308
methods are supposed to return a newly allocated and
309
initialized instance, as would be generated by an
310
<code>alloc</code> method followed by a corresponding
311
<code>init...</code> method. Please note that if you are
312
not using a garbage collector, this means that instances
313
generated by the <code>new...</code> methods are not
314
autoreleased, that is, you are responsible for releasing
315
(autoreleasing) the instances yourself. So when you use
316
<code>new</code> you typically do something like:
322
NSMutableArray *array = AUTORELEASE ([NSMutableArray new]);
328
You do no normally need to override <code>new</code> in
329
subclasses, because if you override <code>init</code> (and
330
optionally <code>allocWithZone:</code> if you really
331
need), <code>new</code> will automatically use your
337
You might need instead to define new <code>new...</code>
338
methods specific to your subclass to match any
339
<code>init...</code> specific to your subclass. For
340
example, if your subclass defines
345
<code>-initWithName:</code>
350
it might be handy for you to have a
355
<code>+newWithName:</code>
360
which combines <code>alloc</code> and
361
<code>initWithName:</code>. You would implement it as follows:
367
+ (id) newWithName: (NSString *)aName
369
return [[self alloc] initWithName: aName];
376
<h3><a name ="method-17">poseAsClass:</a></h3>
377
+ (void) <b>poseAsClass:</b> (Class)aClass;<br>
381
<h3><a name ="method-18">setVersion:</a></h3>
382
+ (void) <b>setVersion:</b> (int)aVersion;<br>
386
<h3><a name ="method-19">superclass</a></h3>
387
+ (Class) <b>superclass</b>;<br>
391
<h3><a name ="method-20">version</a></h3>
392
+ (int) <b>version</b>;<br>
396
<hr><h2>Instances Methods </h2>
397
<h3><a name ="method-21">awakeAfterUsingCoder:</a></h3>
398
- (id) <b>awakeAfterUsingCoder:</b> (NSCoder*)aDecoder;<br>
402
<h3><a name ="method-22">classForArchiver</a></h3>
403
- (Class) <b>classForArchiver</b>;<br>
407
<h3><a name ="method-23">classForCoder</a></h3>
408
- (Class) <b>classForCoder</b>;<br>
412
<h3><a name ="method-24">classForPortCoder</a></h3>
413
- (Class) <b>classForPortCoder</b>;<br>
417
<h3><a name ="method-25">connection:handleRequest:</a></h3>
418
- (BOOL) <b>connection:</b> (NSConnection*)connection <b>handleRequest:</b> (NSDistantObjectRequest*)doreq;<br>
422
<h3><a name ="method-26">copy</a></h3>
423
- (id) <b>copy</b>;<br>
427
<h3><a name ="method-27">dealloc</a></h3>
428
- (void) <b>dealloc</b>;<br>
432
<code>NSObject</code>'s implementation of this method
433
destroys the receiver, by returning the memory allocated
434
to the receiver to the system. After this method has been
435
called on an instance, you must not refer the instance in
436
any way, because it does not exist any longer. If you do,
437
it is a bug and your program might even crash with a
443
If you have turned on the debugging facilities for
444
instance allocation, <code>NSObject</code>'s
445
implementation of this method will also update the various
446
counts and monitors of allocated instances (see the
447
<code>GSDebugAllocation...</code> functions for more
453
Normally you are supposed to manage the memory taken by
454
objects by using the high level interface provided by the
455
<code>retain</code>, <code>release</code> and
456
<code>autorelease</code> methods (or better by the
457
corresponding macros <code>RETAIN</code>,
458
<code>RELEASE</code> and <code>AUTORELEASE</code>), and by
459
autorelease pools and such; whenever the
460
release/autorelease mechanism determines that an object is
461
no longer needed (which happens when its retain count
462
reaches 0), it will call the <code>dealloc</code> method
463
to actually deallocate the object. This means that normally,
464
you should not need to call <code>dealloc</code> directly as
465
the gnustep base library automatically calls it for you when
466
the retain count of an object reaches 0.
471
Because the <code>dealloc</code> method will be called
472
when an instance is being destroyed, if instances of your
473
subclass use objects or resources (as it happens for most
474
useful classes), you must override <code>dealloc</code> in
475
subclasses to release all objects and resources which are
476
used by the instance, otherwise these objects and
477
resources would be leaked. In the subclass
478
implementation, you should first release all your subclass
479
specific objects and resources, and then invoke super's
480
implementation (which will do the same, and so on up in
481
the class hierarchy to <code>NSObject</code>'s
482
implementation, which finally destroys the object). Here
483
is an example of the implementation of
484
<code>dealloc</code> for a subclass whose instances have a
485
single instance variable <code>name</code> which needs to
486
be released when an instance is deallocated:
502
<code>dealloc</code> might contain code to release not
503
only objects, but also other resources, such as open
504
files, network connections, raw memory allocated in other
510
<h3><a name ="method-28">description</a></h3>
511
- (NSString*) <b>description</b>;<br>
515
The <code>description</code> method describes the reciever.
516
In the default GNUstep implementation, this returns an NSString
517
object giving the name of the class of the receiver and the
518
hexadecimal representation of the recievers address enclosed
524
Generally, classes other than NSObject will override this method
525
to provide a more informative description of their instances.
530
<h3><a name ="method-29">descriptionWithLocale:</a></h3>
531
- (NSString*) <b>descriptionWithLocale:</b> (NSDictionary*)aLocale;<br>
532
Standards: GNUstep<br>
534
The default (NSOBject) implementation of this method simply calls
535
the <code>description</code> method and discards the locale
539
<h3><a name ="method-30">descriptionWithLocale:indent:</a></h3>
540
- (NSString*) <b>descriptionWithLocale:</b> (NSDictionary*)aLocale <b>indent:</b> (unsigned int)level;<br>
541
Standards: GNUstep<br>
543
The default (NSObject) implementation of this method simply calls
544
the <code>descriptionWithLocale:</code> method and discards the
548
<h3><a name ="method-31">descriptionWithLocale:indent:to:</a></h3>
549
- (NSString*) <b>descriptionWithLocale:</b> (NSDictionary*)aLocale <b>indent:</b> (unsigned int)level <b>to:</b> (id<GNUDescriptionDestination>)output;<br>
550
Standards: GNUstep<br>
552
The default (NSObject) implementation of this method simply calls
553
the <code>descriptionWithLocale:indent:</code> method and appends
554
the value returned by that method to the output object.
557
Property-list classes (NSString, NSArray, NSDictionary and
558
NSData) will override this method in order to efficiently
559
write descriptions of themselves into property lists.
564
<h3><a name ="method-32">doesNotRecognizeSelector:</a></h3>
565
- (void) <b>doesNotRecognizeSelector:</b> (SEL)aSelector;<br>
569
<h3><a name ="method-33">forwardInvocation:</a></h3>
570
- (void) <b>forwardInvocation:</b> (NSInvocation*)anInvocation;<br>
574
<h3><a name ="method-34">hash</a></h3>
575
- (unsigned int) <b>hash</b>;<br>
579
<h3><a name ="method-35">init</a></h3>
580
- (id) <b>init</b>;<br>
584
<h3><a name ="method-36">isEqual:</a></h3>
585
- (BOOL) <b>isEqual:</b> (id)anObject;<br>
589
<h3><a name ="method-37">methodForSelector:</a></h3>
590
- (IMP) <b>methodForSelector:</b> (SEL)aSelector;<br>
594
<h3><a name ="method-38">methodSignatureForSelector:</a></h3>
595
- (NSMethodSignature*) <b>methodSignatureForSelector:</b> (SEL)aSelector;<br>
597
Returns a method signature object representing the
598
method implemented in this object which corresponds to
599
the supplied selector.
602
<h3><a name ="method-39">mutableCopy</a></h3>
603
- (id) <b>mutableCopy</b>;<br>
607
<h3><a name ="method-40">performSelector:withObject:</a></h3>
608
- (void) <b>performSelector:</b> (SEL)aSelector <b>withObject:</b> (id)anArgument;<br>
612
<h3><a name ="method-41">performSelector:withObject:afterDelay:inModes:</a></h3>
613
- (void) <b>performSelector:</b> (SEL)aSelector <b>withObject:</b> (id)anArgument <b>afterDelay:</b> (NSTimeInterval)delay <b>inModes:</b> (NSArray*)modes;<br>
617
<h3><a name ="method-42">replacementObjectForArchiver:</a></h3>
618
- (id) <b>replacementObjectForArchiver:</b> (NSArchiver*)anArchiver;<br>
622
<h3><a name ="method-43">replacementObjectForCoder:</a></h3>
623
- (id) <b>replacementObjectForCoder:</b> (NSCoder*)aCoder;<br>
627
<h3><a name ="method-44">replacementObjectForPortCoder:</a></h3>
628
- (id) <b>replacementObjectForPortCoder:</b> (NSPortCoder*)aCoder;<br>
632
<h3><a name ="method-45">deallocNotificationsActive</a></h3>
633
- (BOOL) <b>deallocNotificationsActive</b>;<br>
637
Returns a boolean indicating whether deallocation notification
643
If deallocation notification is turned on, whenever an objects
644
retain count reaches zero (and it would normally be deallocated)
645
the <em>_dealloc</em> methiod is called and the object is only
646
deallocated if that method returns YES.
651
<h3><a name ="method-46">setDeallocNotificationsActive:</a></h3>
652
- (void) <b>setDeallocNotificationsActive:</b> (BOOL)flag;<br>
656
Turns on (or off) deallocation notification.
661
If deallocation notification is turned on, whenever an objects
662
retain count reaches zero (and it would normally be deallocated)
663
the <em>_dealloc</em> methiod is called and the object is only
664
deallocated if that method returns YES.
669
<h3><a name ="method-47">_dealloc</a></h3>
670
- (BOOL) <b>_dealloc</b>;<br>
674
Returns a boolean indicating whether the receiver should be
675
deallocated or not. The default implementation returns YES.
680
This method is called when an objects retain count reaches
681
zero (if deallocation notifications are active).
686
This method is provided so that you can take some sort of
687
action when objects are deallocated ... normally you would
688
override this method in your classes, but you could also
689
override it in a category of NSObject - and perform some
690
action whenever <em>any</em> object is deallocated.