~ubuntu-branches/ubuntu/edgy/gnustep-base/edgy

« back to all changes in this revision

Viewing changes to Documentation/gsdoc/NSFunctions.gsdoc

  • Committer: Bazaar Package Importer
  • Author(s): Eric Heintzmann
  • Date: 2004-04-06 15:21:11 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040406152111-95pwmfi50qp1s594
Tags: 1.9.1-2
* debian/rules: Update CC.
* Source/NSUserDefaults: apply patch from CVS.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?xml version="1.0"?>
2
 
<!DOCTYPE gsdoc PUBLIC "-//GNUstep//DTD gsdoc 0.6.6//EN" "http://www.gnustep.org/gsdoc-0_6_6.xml">
3
 
<gsdoc base="NSFunctions" up="Base">
4
 
  <head>
5
 
    <title>NSFunctions</title>
6
 
    <author name="Richard Frith-Macdonald">
7
 
      <email address="rfm@gnu.org"/>
8
 
      <url url="http://www.gnustep.org/developers/whoiswho.html"/>
9
 
    </author>
10
 
    <author name="Nicola Pero">
11
 
      <email address="n.pero@mi.flashnet.it"/>
12
 
      <url url="http://www.gnustep.org/developers/whoiswho.html"/>
13
 
    </author>
14
 
    <version>$Revision: 1.6 $</version>
15
 
    <date>$Date: 2001/08/31 11:00:47 $</date>
16
 
  </head>
17
 
  <body>
18
 
    <chapter>
19
 
      <heading>Functions</heading>
20
 
      <section>
21
 
        <heading>Thread Function</heading>
22
 
      <function name="GSCurrentThread" type="NSThread*">
23
 
        <declared>Foundation/NSThread.h</declared>
24
 
        <desc>
25
 
          <p>
26
 
            This function is a GNUstep extension.  It pretty much
27
 
            duplicates the functionality of [NSThread +currentThread]
28
 
            but is more efficient and is used internally throughout
29
 
            GNUstep.
30
 
          </p>
31
 
          <p>
32
 
            Returns the current thread.  Could perhaps return <code>nil</code>
33
 
            if executing a thread that was started outside the GNUstep
34
 
            environment and not registered (this should not happen in a
35
 
            well-coded application).
36
 
          </p>
37
 
        </desc>
38
 
        <standards><NotOpenStep/><NotMacOS-X/><GNUstep/></standards>
39
 
      </function>
40
 
 
41
 
      <function name="GSRegisterCurrentThread" type="BOOL">
42
 
        <declared>Foundation/NSThread.h</declared>
43
 
        <desc>
44
 
          <p>
45
 
            This function is provided to let threads started by some other
46
 
            software library register themselves to be used with the
47
 
            GNUstep system.  All such threads should call this function
48
 
            before attempting to use any GNUstep objects.
49
 
          </p>
50
 
          <p>
51
 
            Returns <code>YES</code> if the thread can be registered,
52
 
            <code>NO</code> if it is already registered.
53
 
          </p>
54
 
          <p>
55
 
            Sends out a <code>NSWillBecomeMultiThreadedNotification</code>
56
 
            if the process was not already multithreaded.
57
 
          </p>
58
 
        </desc>
59
 
        <standards><NotOpenStep/><NotMacOS-X/><GNUstep/></standards>
60
 
      </function>
61
 
 
62
 
      <function name="GSUnregisterCurrentThread" type="void">
63
 
        <declared>Foundation/NSThread.h</declared>
64
 
        <desc>
65
 
          <p>
66
 
            This function is provided to let threads started by some other
67
 
            software library unregister themselves from the GNUstep threading
68
 
            system.
69
 
          </p>
70
 
          <p>
71
 
            Calling this function causes a
72
 
            <code>NSThreadWillExitNotification</code>
73
 
            to be sent out, and destroys the GNUstep NSThread object
74
 
            associated with the thread.
75
 
          </p>
76
 
        </desc>
77
 
        <standards><NotOpenStep/><NotMacOS-X/><GNUstep/></standards>
78
 
      </function>
79
 
      </section>
80
 
 
81
 
      <section>
82
 
      <heading>Debugging Functions - Counting Instances of Classes</heading>
83
 
      <function name="GSDebugAllocationActive" type="BOOL">
84
 
        <arg type="BOOL">active</arg> 
85
 
        <declared>Foundation/NSDebug.h</declared>
86
 
        <desc>
87
 
          <p>
88
 
            This function is a GNUstep extension.  It activates or
89
 
            deactivates object allocation debugging.  Returns the
90
 
            previous state.  You should call this function to activate
91
 
            allocation debugging before using any of the functions
92
 
            described in this section.  Object allocation debugging
93
 
            should not affect performance too much, and is very useful
94
 
            as it allows you to monitor how many objects of each class
95
 
            your application has allocated.  See below for a detailed
96
 
            description of the info you can get, and why it is useful.
97
 
          </p>
98
 
        </desc>
99
 
        <standards><NotOpenStep/><NotMacOS-X/><GNUstep/></standards>
100
 
      </function>
101
 
 
102
 
      <function name="GSDebugAllocationCount" type="int">
103
 
        <arg type="Class">c</arg> 
104
 
        <declared>Foundation/NSDebug.h</declared>
105
 
        <desc>
106
 
          <p>
107
 
            This function is a GNUstep extension.  Returns the number
108
 
            of instances of the specified class which are currently
109
 
            allocated.  This number is very important to detect memory
110
 
            leaks.  If you notice that this number is constantly
111
 
            increasing without apparent reason, it is very likely a
112
 
            memory leak - you need to check that you are correctly
113
 
            releasing objects of this class, otherwise when your
114
 
            application runs for a long time, it will eventually
115
 
            allocate so many objects as to eat up all your system's
116
 
            memory ...
117
 
          </p>
118
 
 
119
 
          <p> 
120
 
            This function, like the ones below, returns the number of
121
 
            objects allocated/released from the time when
122
 
            GSDebugAllocationActive was first called.  A negative
123
 
            number means that in total, there are less objects of this
124
 
            class allocated now than there were when you called
125
 
            GSDebugAllocationActive; a positive one means there are
126
 
            more.
127
 
          </p>
128
 
        </desc>
129
 
        <standards><NotOpenStep/><NotMacOS-X/><GNUstep/></standards>
130
 
      </function>
131
 
 
132
 
      <function name="GSDebugAllocationPeak" type="int">
133
 
        <arg type="Class">c</arg> 
134
 
        <declared>Foundation/NSDebug.h</declared>
135
 
        <desc>
136
 
          <p>
137
 
            This function is a GNUstep extension.  Returns the peak
138
 
            number of instances of the specified class which have been
139
 
            concurrently allocated.  If this number is very high, it
140
 
            means at some point in time you had a situation with a
141
 
            huge number of objects of this class allocated - this is
142
 
            an indicator that probably at some point in time your
143
 
            application was using a lot of memory - so you might want
144
 
            to investigate whether you can prevent this problem by
145
 
            inserting autorelease pools in your application's
146
 
            processing loops.
147
 
          </p>
148
 
        </desc>
149
 
        <standards><NotOpenStep/><NotMacOS-X/><GNUstep/></standards>
150
 
      </function>
151
 
 
152
 
      <function name="GSDebugAllocationTotal" type="int">
153
 
        <arg type="Class">c</arg> 
154
 
        <declared>Foundation/NSDebug.h</declared>
155
 
        <desc>
156
 
          <p>
157
 
            This function is a GNUstep extension.  Returns the total
158
 
            number of instances of the specified class which have been
159
 
            allocated - basically the number of times you have
160
 
            allocated an object of this class.  If this number is very
161
 
            high, it means you are creating a lot of objects of this
162
 
            class; even if you are releasing them correctly, you must
163
 
            not forget that allocating and deallocating objects is
164
 
            usually one of the slowest things you can do, so you might
165
 
            want to consider whether you can reduce the number of
166
 
            allocations and deallocations that you are doing - for
167
 
            example, by recycling objects of this class, uniquing
168
 
            them, and/or using some sort of flyweight pattern.  It
169
 
            might also be possible that you are unnecessarily creating
170
 
            too many objects of this class.  Well - of course some times 
171
 
            there is nothing you can do about it.
172
 
          </p>
173
 
        </desc>
174
 
        <standards><NotOpenStep/><NotMacOS-X/><GNUstep/></standards>
175
 
      </function>
176
 
 
177
 
      <function name="GSDebugAllocationClassList" type="Class*">
178
 
        <declared>Foundation/NSDebug.h</declared>
179
 
        <desc>
180
 
          <p>
181
 
            This function is a GNUstep extension.  Returns a NULL
182
 
            terminated array listing all the classes for which
183
 
            statistical information has been collected.  Usually, you 
184
 
            call this function, and then loop on all the classes returned, 
185
 
            and for each one you get current, peak and total count by 
186
 
            using GSDebugAllocationCount, GSDebugAllocationPeak and 
187
 
            GSDebugAllocationTotal.
188
 
          </p>
189
 
        </desc>
190
 
        <standards><NotOpenStep/><NotMacOS-X/><GNUstep/></standards>
191
 
      </function>
192
 
 
193
 
      <function name="GSDebugAllocationList" type="const char*">
194
 
        <declared>Foundation/NSDebug.h</declared>
195
 
        <desc>
196
 
          <p>
197
 
            This function is a GNUstep extension.  Returns a newline
198
 
            separated list of the classes which have instances
199
 
            allocated, and the instance counts.  If the 'changeFlag'
200
 
            argument is YES then the list gives the number of
201
 
            instances allocated/deallocated since the function was
202
 
            last called.  This function only returns the current count
203
 
            of instances (not the peak or total count), but its output
204
 
            is ready to be displayed or logged.
205
 
          </p>
206
 
        </desc>
207
 
        <standards><NotOpenStep/><NotMacOS-X/><GNUstep/></standards>
208
 
      </function>
209
 
 
210
 
      <function name="GSDebugAllocationListAll" type="const char*">
211
 
        <declared>Foundation/NSDebug.h</declared>
212
 
        <desc>
213
 
          <p>
214
 
            This function is a GNUstep extension.  Returns a newline
215
 
            separated list of the classes which have had instances
216
 
            allocated at any point, and the total count of the number
217
 
            of instances allocated for each class.  The difference with 
218
 
            GSDebugAllocationList is that this function returns also 
219
 
            classes which have no objects allocated at the moment, but 
220
 
            which had in the past.
221
 
          </p>
222
 
        </desc>
223
 
        <standards><NotOpenStep/><NotMacOS-X/><GNUstep/></standards>
224
 
      </function>
225
 
      </section>
226
 
      
227
 
      <section>
228
 
      <heading>Debugging Functions - Tracking Instances of Classes</heading>
229
 
      <function name="GSDebugAllocationActiveRecordingObjects" type="void">
230
 
        <declared>Foundation/NSDebug.h</declared>
231
 
        <desc>
232
 
          <p>
233
 
            This function is a GNUstep extension.  It activates
234
 
            tracking all allocated instances of the specified class
235
 
            (passed as argument).  This tracking can slow your
236
 
            application down, so you should use it only when you are
237
 
            into serious debugging.  Usually, you will monitor your
238
 
            application by using the functions GSDebugAllocationList
239
 
            and similia (see above), which do not slow things down
240
 
            much and return the number of allocated instances; when
241
 
            (if) by studying the reports generated by these functions
242
 
            you have found a leak of objects of a certain class, and
243
 
            if you can't figure out how to fix it by looking at the
244
 
            code, you can use this function to start tracking
245
 
            allocated instances of that class, and the following one
246
 
            can sometime allow you to list the leaked objects directly.
247
 
          </p>
248
 
        </desc>
249
 
        <standards><NotOpenStep/><NotMacOS-X/><GNUstep/></standards>
250
 
      </function>
251
 
 
252
 
      <function name="GSDebugAllocationListRecordedObjects" type="NSArray *">
253
 
        <arg type="Class">c</arg>
254
 
        <declared>Foundation/NSDebug.h</declared>
255
 
        <desc>
256
 
          <p>
257
 
            This function is a GNUstep extension.  Returns an array
258
 
            containing all the allocated objects of a certain class
259
 
            which have been recorded (to start the recording, you need
260
 
            to invoke GSDebugAllocationActiveRecordedObjects).
261
 
            Presumably, you will immediately call -description on them
262
 
            to find out the objects you are leaking.  The objects are
263
 
            returned in an array, so until the array is autoreleased,
264
 
            the objects are not released.
265
 
          </p>
266
 
        </desc>
267
 
        <standards><NotOpenStep/><NotMacOS-X/><GNUstep/></standards>
268
 
      </function>
269
 
      </section>
270
 
 
271
 
      <section>
272
 
      <heading>Localization Functions and Macros</heading>
273
 
      <function name="NSLocalizedString" type="NSString *">
274
 
        <arg type="NSString *">key</arg>
275
 
        <arg type="NSString *">comment</arg>
276
 
        <declared>Foundation/NSBundle.h</declared>
277
 
        <desc>
278
 
          <p>
279
 
            This function (macro) is used to get the localized
280
 
            translation of the string <code>key</code>.
281
 
            <code>key</code> is looked up in the
282
 
            <code>Localizable.strings</code> file for the current
283
 
            language.  The current language is determined by the
284
 
            available languages in which the application is
285
 
            translated, and by using the <code>NSLanguages</code> user
286
 
            defaults (which should contain an array of the languages
287
 
            preferred by the user, in order of preference).
288
 
         </p>
289
 
         <p>
290
 
            Technically, the function works by calling
291
 
            <code>localizedStringForKey:value:table:</code> on the
292
 
            main bundle, using <code>@""</code> as value, and
293
 
            <code>nil</code> as the table.  The <code>comment</code>
294
 
            is ignored when the macro is expanded; but when we have
295
 
            tools which can generate the
296
 
            <code>Localizable.strings</code> files automatically from
297
 
            source code, the <code>comment</code> will be used by the
298
 
            tools and added as a comment before the string to
299
 
            translate.  Upon finding something like
300
 
          </p>
301
 
          <p>
302
 
            <code>
303
 
               NSLocalizedString (@"My useful string", 
304
 
                                  @"My useful comment about the string");
305
 
            </code>
306
 
          </p>
307
 
          <p>
308
 
            in the source code, the tools will generate the lines 
309
 
          </p>
310
 
          <p>
311
 
            <code>
312
 
               /* My useful comment about the string */
313
 
               " My useful string" = "My useful string";
314
 
            </code>
315
 
          </p>
316
 
          <p>
317
 
            in the <code>Localizable.strings</code> file (the
318
 
            translator then can use this as a skeleton for the
319
 
            <code>Localizable.strings</code> for his/her own language,
320
 
            where she/he can replace the right hand side with the
321
 
            translation in her/his own language).  The comment can
322
 
            help the translator to decide how to translate when it is
323
 
            not clear how to translate (because the original string is
324
 
            now out of context, and out of context might not be so
325
 
            clear what the string means).  The comment is totally
326
 
            ignored by the library code.
327
 
          </p>
328
 
          <p>
329
 
            If you don't have a comment (because the string is so
330
 
            self-explanatory that it doesn't need it), you can leave
331
 
            it blank, by using <code>@""</code> as a comment.  If the
332
 
            string might be unclear out of context, it is recommended
333
 
            that you add a comment (even if it is unused for now).
334
 
          </p>
335
 
        </desc>
336
 
      </function>
337
 
      <function name="NSLocalizedStringFromTable" type="NSString *">
338
 
        <arg type="NSString *">key</arg>
339
 
        <arg type="NSString *">table</arg>
340
 
        <arg type="NSString *">comment</arg>
341
 
        <declared>Foundation/NSBundle.h</declared>
342
 
        <desc>
343
 
          <p>
344
 
            This function (macro) does the same as
345
 
            <code>NSLocalizedString</code>, but uses the table
346
 
            <code>table</code> rather than the default table.  This
347
 
            means that the string to translate will be looked up in a
348
 
            different file than <code>Localizable.strings</code>.  For
349
 
            example, if you pass <code>DatabaseErrors</code> as the
350
 
            <code>table</code>, the string will be looked up for
351
 
            translation in the file
352
 
            <code>DatabaseErrors.strings</code>.  This allows you to
353
 
            have the same string translated in different ways, by
354
 
            having a different translation in different tables, and
355
 
            choosing between the different translation by choosing a
356
 
            different table.
357
 
          </p>
358
 
        </desc>
359
 
      </function>
360
 
      <function name="NSLocalizedStringFromTableInBundle" type="NSString *">
361
 
        <arg type="NSString *">key</arg>
362
 
        <arg type="NSString *">table</arg>
363
 
        <arg type="NSString *">bundle</arg>
364
 
        <arg type="NSString *">comment</arg>
365
 
        <declared>Foundation/NSBundle.h</declared>
366
 
        <desc>
367
 
          <p>
368
 
            This function is the full-blown localization function (it
369
 
            is actually a macro).  It looks up the string
370
 
            <code>key</code> for translation in the table
371
 
            <code>table</code> of the bundle <code>bundle</code>
372
 
            (please refer to the NSBundle documentation for more
373
 
            information on how this lookup is done).
374
 
            <code>comment</code> is a comment, which is ignored by the 
375
 
            library (it is discarded when the macro is expanded) but which 
376
 
            can be used by tools which parse the source code and generate
377
 
            strings table to provide a comment which the translator can
378
 
            use when translating the string.
379
 
          </p>
380
 
        </desc>
381
 
      </function>
382
 
      </section>
383
 
      <section>
384
 
      <heading>GNUstep Localization Functions and Macros</heading>
385
 
      <function name="NSLocalizedStaticString" type="NSString *">
386
 
        <arg type="NSString *">key</arg>
387
 
        <arg type="NSString *">comment</arg>
388
 
        <declared>Foundation/NSBundle.h</declared>
389
 
        <desc>
390
 
          <p>
391
 
            This function (macro) is a GNUstep extensions, and it is used 
392
 
            to localize static strings.  Here is an example of a static 
393
 
            string:
394
 
          </p>
395
 
          <p>
396
 
            <code>
397
 
              NSString *message = @"Hi there";
398
 
 
399
 
              /* ... some code ... */
400
 
 
401
 
              NSLog (message);
402
 
           </code>
403
 
          </p>
404
 
          <p>
405
 
            This string can not be localized using the standard 
406
 
            openstep functions/macros.  By using this gnustep extension,
407
 
            you can localize it as follows:
408
 
          </p>
409
 
          <p>
410
 
            <code>
411
 
              NSString *message = NSLocalizedStaticString (@"Hi there", 
412
 
                                                           @"Greeting");
413
 
 
414
 
              /* ... some code ... */
415
 
 
416
 
              NSLog (NSLocalizedString (message, @""));
417
 
           </code>
418
 
          </p>
419
 
          <p>
420
 
            When the tools generate the
421
 
            <code>Localizable.strings</code> file from the source
422
 
            code, they will ignore the <code>NSLocalizedString</code>
423
 
            call while they will extract the string (and the comment)
424
 
            to localize from the <code>NSLocalizedStaticString</code>
425
 
            call.
426
 
          </p>
427
 
          <p>
428
 
            When the code is compiled, instead, the
429
 
            <code>NSLocalizedStaticString</code> call is ignored (discarded, 
430
 
            it is a macro which simply expands to <code>key</code>), while
431
 
            the <code>NSLocalizedString</code> will actually look up the 
432
 
            string for translation in the <code>Localizable.strings</code>
433
 
            file.
434
 
          </p>
435
 
          <p>
436
 
            Please note that there is currently no macro/function to
437
 
            localize static strings using different tables.  If you
438
 
            need that functionality, you have either to prepare the
439
 
            localization tables by hand, or to rewrite your code in
440
 
            such a way as not to use static strings.
441
 
          </p>
442
 
        </desc>
443
 
        <standards><NotOpenStep/><NotMacOS-X/><GNUstep/></standards>
444
 
      </function>
445
 
      <function name="_" type="NSString *">
446
 
        <arg type="NSString *">key</arg>
447
 
        <declared>Foundation/NSBundle.h</declared>
448
 
        <desc>
449
 
          <p>
450
 
            This function (macro) is a GNUstep extension. 
451
 
          </p>
452
 
          <p>
453
 
            <code>_(@"My string to translate")</code>
454
 
          </p>
455
 
          <p>
456
 
            is exactly the same as 
457
 
          </p>
458
 
          <p>
459
 
            <code>NSLocalizedString (@"My string to translate", @"")</code>
460
 
          </p>
461
 
          <p>
462
 
            It is useful when you need to translate an application
463
 
            very quickly, as you just need to enclose all strings
464
 
            inside <code>_()</code>.  But please note that when you
465
 
            use this macro, you are not taking advantage of comments
466
 
            for the translator, so consider using
467
 
            <code>NSLocalizedString</code> instead when you need a
468
 
            comment.
469
 
          </p>
470
 
        </desc>
471
 
        <standards><NotOpenStep/><NotMacOS-X/><GNUstep/></standards>
472
 
      </function>
473
 
 
474
 
      <function name="__" type="NSString *">
475
 
        <arg type="NSString *">key</arg>
476
 
        <declared>Foundation/NSBundle.h</declared>
477
 
        <desc>
478
 
          <p>
479
 
            This function (macro) is a GNUstep extension. 
480
 
          </p>
481
 
          <p>
482
 
            <code>__(@"My string to translate")</code>
483
 
          </p>
484
 
          <p>
485
 
            is exactly the same as 
486
 
          </p>
487
 
          <p>
488
 
            <code>NSLocalizedStaticString (@"My string to translate", 
489
 
                                           @"")</code>
490
 
          </p>
491
 
          <p>
492
 
            It is useful when you need to translate an application very 
493
 
            quickly.  You would use it as follows for static strings:
494
 
          </p>
495
 
          <p>
496
 
           <code>
497
 
             NSString *message = __(@"Hello there");
498
 
 
499
 
             /* ... more code ... */
500
 
 
501
 
             NSLog (_(messages));
502
 
           </code>
503
 
          </p>
504
 
          <p>
505
 
            But please note that when you use this macro, you are not
506
 
            taking advantage of comments for the translator, so
507
 
            consider using <code>NSLocalizedStaticString</code>
508
 
            instead when you need a comment.
509
 
          </p>
510
 
        </desc>
511
 
        <standards><NotOpenStep/><NotMacOS-X/><GNUstep/></standards>
512
 
      </function>
513
 
 
514
 
      </section>
515
 
    </chapter>
516
 
 
517
 
    <chapter>
518
 
      <heading>Types</heading>
519
 
      <type name="NSRange">
520
 
        <typespec>
521
 
          struct { unsigned long location; unsigned long length; }
522
 
        </typespec>
523
 
        <declared>Foundation/NSRange.h</declared>
524
 
        <desc>
525
 
          <p>
526
 
            The NSRange type is used to specify ranges of locations,
527
 
            typically items in an array, characters in a string, and bytes
528
 
            in a data object.
529
 
          </p>
530
 
          <p>
531
 
            As 'boundary' or 'fencepost' errors are a particularly common
532
 
            problem in programming, it is important that you understand
533
 
            how an NSRange works.
534
 
          </p>
535
 
          <p>
536
 
            An NSRange consists of a location and a length.  The points
537
 
            that are considered to lie in a range are the integers from
538
 
            the location to the location plus the length, so the number
539
 
            of points in a range is the length of the range plus one.<br />
540
 
            However, if you consider these points like the marks on a
541
 
            ruler, you can only store information <strong>between</strong>
542
 
            points.  So the number of items that can be stored in a range
543
 
            is the length of the range.
544
 
          </p>
545
 
        </desc>
546
 
        <standards><OpenStep/><MacOS-X/><GNUstep/></standards>
547
 
      </type>
548
 
    </chapter>
549
 
  </body>
550
 
</gsdoc>