home *** CD-ROM | disk | FTP | other *** search
/ ftp.pasteur.org/FAQ/ / ftp-pasteur-org-FAQ.zip / FAQ / computer-lang / Objective-C / faq
Encoding:
Text File  |  2004-04-17  |  16.1 KB  |  460 lines

  1. Path: senator-bedfellow.mit.edu!bloom-beacon.mit.edu!4.24.21.218.MISMATCH!newsfeed2.dallas1.level3.net!newsfeed3.dallas1.level3.net!news.level3.com!newsfeed1.easynews.com!easynews.com!easynews!c03.atl99!news.usenetserver.com!newsfeed.online.be!feed1.news.be.easynet.net!ossa.telenet-ops.be!phobos.telenet-ops.be.POSTED!not-for-mail
  2. From: stes@pandora.be
  3. Newsgroups: comp.lang.objective-c,comp.answers,news.answers
  4. Subject: Objective-C FAQ
  5. Supersedes: <objcfaq_1079463601@news.pandora.be>
  6. Followup-To: poster
  7. Organization: David Stes
  8. Lines: 442
  9. Sender: stes@D5E02AFE.kabel.telenet.be
  10. Approved: news-answers-request@MIT.EDU
  11. Expires: 30 May 2004 18:00:02 GMT
  12. Message-ID: <objcfaq_1082138402@news.pandora.be>
  13. Date: Fri, 16 Apr 2004 17:51:56 GMT
  14. NNTP-Posting-Host: 213.224.42.254
  15. X-Complaints-To: abuse@telenet.be
  16. X-Trace: phobos.telenet-ops.be 1082137916 213.224.42.254 (Fri, 16 Apr 2004 19:51:56 MEST)
  17. NNTP-Posting-Date: Fri, 16 Apr 2004 19:51:56 MEST
  18. Xref: senator-bedfellow.mit.edu comp.lang.objective-c:23966 comp.answers:56841 news.answers:269597
  19.  
  20. Archive-name: computer-lang/Objective-C/faq
  21. Posting-Frequency: monthly
  22.  
  23.              Frequently Asked Questions - comp.lang.objective-c
  24.                                       
  25.                   compiled by David Stes (stes@pandora.be)
  26.    
  27.                                  April 16 2004
  28.    
  29. Contents
  30.  
  31.      * Contents
  32.      * 1. About this FAQ
  33.           + 1.1 Where can I find the latest version of the FAQ ?
  34.      * 2. Objective-C Compiler Commands
  35.           + 2.1 What's the file suffix for Objective-C source ?
  36.           + 2.2 How do I compile .m files with the Stepstone compiler ?
  37.           + 2.3 How do I compile .m files with the Apple compiler ?
  38.           + 2.4 How do I compile .m files with the GNU C compiler ?
  39.           + 2.5 How do I compile .m files with the POC ?
  40.      * 3. Objective-C preprocessor issues
  41.           + 3.1 What's the syntax for comments ?
  42.           + 3.2 How do I include the root class ?
  43.           + 3.3 What is #import ?
  44.           + 3.4 Why am I lectured about using #import ?
  45.      * 4. Object datatype (id)
  46.           + 4.1 What is id ?
  47.           + 4.2 What is the difference between self and super ?
  48.           + 4.3 What is @defs() ?
  49.      * 5. Message selectors (SEL)
  50.           + 5.1 What is a SEL ?
  51.           + 5.2 What is perform: doing ?
  52.           + 5.3 How do I know the SEL of a given method ?
  53.      * 6. Implementation pointers (IMP)
  54.           + 6.1 What is an IMP ?
  55.           + 6.2 How do I get an IMP given a SEL ?
  56.           + 6.3 How do I send a message given an IMP ?
  57.           + 6.4 How can I use IMP for methods returning double ?
  58.           + 6.5 Can I use perform: for a message returning double ?
  59.      * 7. Copying objects
  60.           + 7.1 What's the difference between copy and deepCopy ?
  61.      * 8. Objective-C and C++
  62.           + 8.1 How can I link a C++ library into an Objective-C program
  63.             ?
  64.      * 9. Messages
  65.           + 9.1 How do I make a static method ?
  66.           + 9.2 How do I prevent an object from sending a given message ?
  67.           + 9.3 Do I have to recompile everything if I change the
  68.             implementation of a method ?
  69.      * 10. Instance and Class Variables
  70.           + 10.1 Do I have to recompile everything if I change instance
  71.             variables of a class ?
  72.      * 11. Objective-C and X-Windows
  73.           + 11.1 How do I include X Intrinsics headers into an
  74.             Objective-C file ?
  75.      * 12. Stepstone Specific Questions
  76.           + 12.1 How do I allocate an object on the stack ?
  77.      * 13. GNU Objective-C Specific Questions
  78.           + 13.1 Why do I get a 'floating point exception' ?
  79.      * 14. Apple Objective-C Specific Questions
  80.           + 14.1 What's the class of a constant string ?
  81.           + 14.2 How can I link a C++ library into an Objective-C program
  82.             ?
  83.      * 15. Portable Object Compiler Objective-C Specific Questions
  84.           + 15.1 What's the syntax for class variables ?
  85.           + 15.2 How do I forward messages ?
  86.           + 15.3 How can I link a C++ library into an Objective-C program
  87.             ?
  88.      * 16. Books and further reading
  89.           + 16.1 Object-Oriented Programming : An Evolutionary Approach,
  90.             2nd Ed.
  91.           + 16.2 An Introduction To Object-Oriented Programming, 2nd Ed.
  92.           + 16.3 Objective-C : Object-Oriented Programming Techniques
  93.           + 16.4 Applications of Object-Oriented Programming; C++
  94.             SmallTalk Actor Objective-C Object PASCAL
  95.        
  96.                                1. About this FAQ
  97.                                        
  98. 1.1 Where can I find the latest version of the FAQ ?
  99.  
  100.    It's posted once a month to comp.lang.objective-c, comp.answers and
  101.    news.answers. It is archived at
  102.    ftp://rtfm.mit.edu/pub/faqs/computer-lang/Objective-C/faq.
  103.    
  104.                        2. Objective-C Compiler Commands
  105.                                        
  106. 2.1 What's the file suffix for Objective-C source ?
  107.  
  108.    It's .m for implementation files, and .h for header files. Objective-C
  109.    compilers usually also accept .c as a suffix, but compile those files
  110.    in plain C mode.
  111.    
  112. 2.2 How do I compile .m files with the Stepstone compiler ?
  113.  
  114. objcc -c class.m
  115. objcc -o class class.o
  116.  
  117.    See http://www.stepstn.com for more information.
  118.    
  119. 2.3 How do I compile .m files with the Apple compiler ?
  120.  
  121. cc -c class.m
  122. cc -o class class.o
  123.  
  124.    See http://www.apple.com for more information.
  125.    
  126. 2.4 How do I compile .m files with the GNU C compiler ?
  127.  
  128. gcc -c class.m
  129. gcc -o class class.o -lobjc -lpthread
  130.  
  131.    See http://www.gnu.org for more information.
  132.    
  133. 2.5 How do I compile .m files with the POC ?
  134.  
  135. objc -c class.m
  136. objc -o class class.o
  137.  
  138.    See http://metalab.unc.edu/pub/Linux/devel/lang/objc/ for more
  139.    information.
  140.    
  141.                       3. Objective-C preprocessor issues
  142.                                        
  143. 3.1 What's the syntax for comments ?
  144.  
  145.    The Objective-C preprocessor usually supports two styles of comments :
  146.    
  147. // this is a BCPL-style comment (extends to end of line)
  148.  
  149.    and
  150.    
  151. /* this is a C-style comment */
  152.  
  153. 3.2 How do I include the root class ?
  154.  
  155.    On Stepstone and the POC, the header file to include is :
  156.    
  157. <Object.h>
  158.  
  159.    On GNU cc and Apple cc, it's :
  160.    
  161. <objc/Object.h>
  162.  
  163.    The root class is located in a directory called runtime for the
  164.    Stepstone compiler, and in a directory called objcrt for the POC, but
  165.    because of implicit -I options passed on to the preprocessor, these
  166.    locations are automatically searched.
  167.    
  168. 3.3 What is #import ?
  169.  
  170.    It's a C preprocessor construct to avoid multiple inclusions of the
  171.    same file.
  172.    
  173. #import <Object.h>
  174.  
  175.    is an alternative to
  176.    
  177. #include <Object.h>
  178.  
  179.    where the .h file is protected itself against multiple inclusions :
  180.    
  181. #ifndef _OBJECT_H_
  182. ...
  183. #define _OBJECT_H_
  184. #endif
  185.  
  186. 3.4 Why am I lectured about using #import ?
  187.  
  188.    The GNU Objective-C compiler emits a warning when you use #import
  189.    because some people find using #import poor style. You can turn off
  190.    the warning by using the -Wno-import option, you could modify the
  191.    compiler source code and set the variable warn_import (in the file
  192.    cccp.c) or you could convert your code to use pairs of #ifndef and
  193.    #endif, as shown above, which makes your code work with all compilers.
  194.    
  195.                             4. Object datatype (id)
  196.                                        
  197. 4.1 What is id ?
  198.  
  199.    It's a generic C type that Objective-C uses for an arbitrary object.
  200.    For example, a static function that takes one object as argument and
  201.    returns an object, could be declared as :
  202.    
  203. static id myfunction(id argument) { ... }
  204.  
  205. 4.2 What is the difference between self and super ?
  206.  
  207.    self is a variable that refers to the object that received a message
  208.    in a method implementation. super refers to the same variable, but
  209.    directs the compiler to use a method implementation from the
  210.    superclass.
  211.    
  212.    Using pseudo-code, where copy (from super) is the syntax for the copy
  213.    implementation of the superclass, the following are equivalent :
  214.    
  215. myObject = [super copy];
  216.  
  217.    and,
  218.    
  219. myObject = [self copy (from super)]; // pseudo-code
  220.  
  221. 4.3 What is @defs() ?
  222.  
  223.    It's a compiler directive to get access to the internal memory layout
  224.    of instances of a particular class.
  225.    
  226. typedef struct { @defs(MyClass) } *TMyClass;
  227.  
  228.    defines a C-type TMyClass with a memory layout that is the same as
  229.    that of MyClass instances.
  230.    
  231.                           5. Message selectors (SEL)
  232.                                        
  233. 5.1 What is a SEL ?
  234.  
  235.    It's the C type of a message selector; it's often defined as a
  236.    (uniqued) string of characters (the name of the method, including
  237.    colons), but not all compilers define the type as such.
  238.    
  239. 5.2 What is perform: doing ?
  240.  
  241.    perform: is a message to send a message, identified by its message
  242.    selector (SEL), to an object.
  243.    
  244. 5.3 How do I know the SEL of a given method ?
  245.  
  246.    If the name of the method is known at compile time, use @selector :
  247.    
  248. [myObject perform:@selector(close)];
  249.  
  250.    At runtime, you can lookup the selector by a runtime function that
  251.    takes the name of the message as argument, as in :
  252.    
  253. SEL mySel = selUid(name); // for Stepstone
  254. SEL mySel = sel_getUid(name); // for Apple
  255. SEL mySel = sel_get_any_uid(name); // for GNU Objective C
  256. SEL mySel = selUid(name); // for POC
  257.  
  258.                        6. Implementation pointers (IMP)
  259.                                        
  260. 6.1 What is an IMP ?
  261.  
  262.    It's the C type of a method implementation pointer, a function pointer
  263.    to the function that implements an Objective-C method. It is defined
  264.    to return id and takes two hidden arguments, self and _cmd :
  265.    
  266. typedef id (*IMP)(id self,SEL _cmd,...);
  267.  
  268. 6.2 How do I get an IMP given a SEL ?
  269.  
  270.    This can be done by sending a methodFor: message :
  271.    
  272. IMP myImp = [myObject methodFor:mySel];
  273.  
  274. 6.3 How do I send a message given an IMP ?
  275.  
  276.    By dereferencing the function pointer. The following are all
  277.    equivalent :
  278.    
  279. [myObject myMessage];
  280.  
  281.    or
  282.    
  283. IMP myImp = [myObject methodFor:@selector(myMessage)];
  284. myImp(myObject,@selector(myMessage));
  285.  
  286.    or
  287.    
  288. [myObject perform:@selector(myMessage)];
  289.  
  290. 6.4 How can I use IMP for methods returning double ?
  291.  
  292.    For methods that return a C type such as double instead of id, the IMP
  293.    function pointer is casted from pointer to a function returning id to
  294.    pointer to a function returning double :
  295.    
  296. double aDouble = ((double (*) (id,SEL))myImp)(self,_cmd);
  297.  
  298. 6.5 Can I use perform: for a message returning double ?
  299.  
  300.    No. The method perform: is for sending messages returning id without
  301.    any other argument. Use perform:with: if the message returns id and
  302.    takes one argument. Use methodFor: for the general case of any number
  303.    of arguments and any return type.
  304.    
  305.                               7. Copying objects
  306.                                        
  307. 7.1 What's the difference between copy and deepCopy ?
  308.  
  309.    copy is intented to make a bytecopy of the object, sharing pointers
  310.    with the original, and can be overridden to copy additional memory.
  311.    deepCopy is intented to make a copy that doesn't share pointers with
  312.    the original. A deep copy of an object contains copies of its instance
  313.    variables, while a plain copy is normally just a copy at the first
  314.    level.
  315.    
  316.                             8. Objective-C and C++
  317.                                        
  318. 8.1 How can I link a C++ library into an Objective-C program ?
  319.  
  320.    You have two options : either use the Apple compiler or use the POC.
  321.    The former accepts a mix of C++ and Objective-C syntax (called
  322.    Objective-C++), the latter compiles Objective-C into C and then
  323.    compiles the intermediate code with a C++ compiler. See the compiler
  324.    specific questions for more information.
  325.    
  326.                                   9. Messages
  327.                                        
  328. 9.1 How do I make a static method ?
  329.  
  330.    Methods are always implemented in Objective-C as static functions. The
  331.    only way to obtain the IMP (implementation pointer) of a method is
  332.    through the runtime (via methodFor: and friends), because the function
  333.    itself is static to the file that implements the method.
  334.    
  335. 9.2 How do I prevent an object from sending a given message ?
  336.  
  337.    You can't. If your object responds to a message, any other class can
  338.    send this message. You could add an extra argument sender and check,
  339.    as in :
  340.    
  341. - mymethod:sender
  342. {
  343.   if ([sender isKindOf:..]) ...
  344. }
  345.  
  346.    But this still requires cooperation of the sender, to use a correct
  347.    argument :
  348.    
  349.   [anObject mymethod:self];
  350.  
  351. 9.3 Do I have to recompile everything if I change the implementation of a
  352. method ?
  353.  
  354.    No, you only have to recompile the implementation of the method
  355.    itself. Files that only send that particular messages do not have to
  356.    be recompiled because Objective-C has dynamic binding.
  357.    
  358.                        10. Instance and Class Variables
  359.                                        
  360. 10.1 Do I have to recompile everything if I change instance variables of a
  361. class ?
  362.  
  363.    You have to recompile that class, all of its subclasses, and those
  364.    files that use @defs() or use direct access to the instance variables
  365.    of that class. In short, using @defs() to access instance variables,
  366.    or accessing instance variables through subclassing, breaks the
  367.    encapsulation that the Objective-C runtime normally provides for all
  368.    other files (the files that you do not have to recompile).
  369.    
  370.                          11. Objective-C and X-Windows
  371.                                        
  372. 11.1 How do I include X Intrinsics headers into an Objective-C file ?
  373.  
  374.    To avoid a conflict between Objective-C's Object and the X11/Object,
  375.    do the following :
  376.    
  377. #include <Object.h>
  378. #define Object XtObject
  379. #include <X11/Intrinsic.h>
  380. #include <X11/IntrinsicP.h>
  381. #undef Object
  382.  
  383.                        12. Stepstone Specific Questions
  384.                                        
  385. 12.1 How do I allocate an object on the stack ?
  386.  
  387.    To allocate an instance of 'MyClass' on the stack :
  388.    
  389. MyClass aClass = [MyClass new];
  390.  
  391.                     13. GNU Objective-C Specific Questions
  392.                                        
  393. 13.1 Why do I get a 'floating point exception' ?
  394.  
  395.    This used to happen on some platforms and is described at
  396.    ftp://ftp.ics.ele.tue.nl/pub/users/tiggr/objc/README.387. A solution
  397.    was to add -lieee to the command line, so that an invalid floating
  398.    point operation in the runtime did not send a signal. DJGPP users can
  399.    consult http://www.delorie.com/djgpp/v2faq/. AIX users may want to
  400.    consult http://world.std.com/~gsk/oc-rs6000-problems.html. In some
  401.    cases, you can fix the problem by upgrading to a more recent version
  402.    of the GNU Objective-C runtime and/or compiler.
  403.    
  404.                    14. Apple Objective-C Specific Questions
  405.                                        
  406. 14.1 What's the class of a constant string ?
  407.  
  408.    It's an NXConstantString.
  409.    
  410. NXConstantString *myString = @"my string";
  411.  
  412. 14.2 How can I link a C++ library into an Objective-C program ?
  413.  
  414. c++ -c file.m
  415. c++ file.o -lcpluslib -o myprogram
  416.  
  417.           15. Portable Object Compiler Objective-C Specific Questions
  418.                                        
  419. 15.1 What's the syntax for class variables ?
  420.  
  421.    List the class variables after the instance variables, and group them
  422.    together in the same way as instance variables, as follows :
  423.    
  424. @implementation MyClass : Object { id ivar1; int ivar2; } : { id cvar1; }
  425. @end
  426.  
  427. 15.2 How do I forward messages ?
  428.  
  429.    You have to implement doesNotUnderstand: to send a sentTo: message.
  430.    
  431. - doesNotUnderstand:aMsg
  432. {
  433.   return [aMsg sentTo:aProxy];
  434. }
  435.  
  436. 15.3 How can I link a C++ library into an Objective-C program ?
  437.  
  438. objc -c -cplus file.m
  439. objc -cplus file.o -lcpluslib -o myprogram
  440.  
  441.                          16. Books and further reading
  442.                                        
  443. 16.1 Object-Oriented Programming : An Evolutionary Approach, 2nd Ed.
  444.  
  445.    Brad Cox & Andy Novobilski, ISBN 0201548348.
  446.    
  447. 16.2 An Introduction To Object-Oriented Programming, 2nd Ed.
  448.  
  449.    Timothy Budd, ISBN 0201824191
  450.    
  451. 16.3 Objective-C : Object-Oriented Programming Techniques
  452.  
  453.    Pinson, Lewis J. / Wiener, Richard S., ISBN 0201508281
  454.    
  455. 16.4 Applications of Object-Oriented Programming; C++ SmallTalk Actor
  456. Objective-C Object PASCAL
  457.  
  458.    Pinson, Lewis J. / Wiener, Richard S., ISBN 0201503697
  459.      _________________________________________________________________
  460.