home *** CD-ROM | disk | FTP | other *** search
- 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
- From: stes@pandora.be
- Newsgroups: comp.lang.objective-c,comp.answers,news.answers
- Subject: Objective-C FAQ
- Supersedes: <objcfaq_1079463601@news.pandora.be>
- Followup-To: poster
- Organization: David Stes
- Lines: 442
- Sender: stes@D5E02AFE.kabel.telenet.be
- Approved: news-answers-request@MIT.EDU
- Expires: 30 May 2004 18:00:02 GMT
- Message-ID: <objcfaq_1082138402@news.pandora.be>
- Date: Fri, 16 Apr 2004 17:51:56 GMT
- NNTP-Posting-Host: 213.224.42.254
- X-Complaints-To: abuse@telenet.be
- X-Trace: phobos.telenet-ops.be 1082137916 213.224.42.254 (Fri, 16 Apr 2004 19:51:56 MEST)
- NNTP-Posting-Date: Fri, 16 Apr 2004 19:51:56 MEST
- Xref: senator-bedfellow.mit.edu comp.lang.objective-c:23966 comp.answers:56841 news.answers:269597
-
- Archive-name: computer-lang/Objective-C/faq
- Posting-Frequency: monthly
-
- Frequently Asked Questions - comp.lang.objective-c
-
- compiled by David Stes (stes@pandora.be)
-
- April 16 2004
-
- Contents
-
- * Contents
- * 1. About this FAQ
- + 1.1 Where can I find the latest version of the FAQ ?
- * 2. Objective-C Compiler Commands
- + 2.1 What's the file suffix for Objective-C source ?
- + 2.2 How do I compile .m files with the Stepstone compiler ?
- + 2.3 How do I compile .m files with the Apple compiler ?
- + 2.4 How do I compile .m files with the GNU C compiler ?
- + 2.5 How do I compile .m files with the POC ?
- * 3. Objective-C preprocessor issues
- + 3.1 What's the syntax for comments ?
- + 3.2 How do I include the root class ?
- + 3.3 What is #import ?
- + 3.4 Why am I lectured about using #import ?
- * 4. Object datatype (id)
- + 4.1 What is id ?
- + 4.2 What is the difference between self and super ?
- + 4.3 What is @defs() ?
- * 5. Message selectors (SEL)
- + 5.1 What is a SEL ?
- + 5.2 What is perform: doing ?
- + 5.3 How do I know the SEL of a given method ?
- * 6. Implementation pointers (IMP)
- + 6.1 What is an IMP ?
- + 6.2 How do I get an IMP given a SEL ?
- + 6.3 How do I send a message given an IMP ?
- + 6.4 How can I use IMP for methods returning double ?
- + 6.5 Can I use perform: for a message returning double ?
- * 7. Copying objects
- + 7.1 What's the difference between copy and deepCopy ?
- * 8. Objective-C and C++
- + 8.1 How can I link a C++ library into an Objective-C program
- ?
- * 9. Messages
- + 9.1 How do I make a static method ?
- + 9.2 How do I prevent an object from sending a given message ?
- + 9.3 Do I have to recompile everything if I change the
- implementation of a method ?
- * 10. Instance and Class Variables
- + 10.1 Do I have to recompile everything if I change instance
- variables of a class ?
- * 11. Objective-C and X-Windows
- + 11.1 How do I include X Intrinsics headers into an
- Objective-C file ?
- * 12. Stepstone Specific Questions
- + 12.1 How do I allocate an object on the stack ?
- * 13. GNU Objective-C Specific Questions
- + 13.1 Why do I get a 'floating point exception' ?
- * 14. Apple Objective-C Specific Questions
- + 14.1 What's the class of a constant string ?
- + 14.2 How can I link a C++ library into an Objective-C program
- ?
- * 15. Portable Object Compiler Objective-C Specific Questions
- + 15.1 What's the syntax for class variables ?
- + 15.2 How do I forward messages ?
- + 15.3 How can I link a C++ library into an Objective-C program
- ?
- * 16. Books and further reading
- + 16.1 Object-Oriented Programming : An Evolutionary Approach,
- 2nd Ed.
- + 16.2 An Introduction To Object-Oriented Programming, 2nd Ed.
- + 16.3 Objective-C : Object-Oriented Programming Techniques
- + 16.4 Applications of Object-Oriented Programming; C++
- SmallTalk Actor Objective-C Object PASCAL
-
- 1. About this FAQ
-
- 1.1 Where can I find the latest version of the FAQ ?
-
- It's posted once a month to comp.lang.objective-c, comp.answers and
- news.answers. It is archived at
- ftp://rtfm.mit.edu/pub/faqs/computer-lang/Objective-C/faq.
-
- 2. Objective-C Compiler Commands
-
- 2.1 What's the file suffix for Objective-C source ?
-
- It's .m for implementation files, and .h for header files. Objective-C
- compilers usually also accept .c as a suffix, but compile those files
- in plain C mode.
-
- 2.2 How do I compile .m files with the Stepstone compiler ?
-
- objcc -c class.m
- objcc -o class class.o
-
- See http://www.stepstn.com for more information.
-
- 2.3 How do I compile .m files with the Apple compiler ?
-
- cc -c class.m
- cc -o class class.o
-
- See http://www.apple.com for more information.
-
- 2.4 How do I compile .m files with the GNU C compiler ?
-
- gcc -c class.m
- gcc -o class class.o -lobjc -lpthread
-
- See http://www.gnu.org for more information.
-
- 2.5 How do I compile .m files with the POC ?
-
- objc -c class.m
- objc -o class class.o
-
- See http://metalab.unc.edu/pub/Linux/devel/lang/objc/ for more
- information.
-
- 3. Objective-C preprocessor issues
-
- 3.1 What's the syntax for comments ?
-
- The Objective-C preprocessor usually supports two styles of comments :
-
- // this is a BCPL-style comment (extends to end of line)
-
- and
-
- /* this is a C-style comment */
-
- 3.2 How do I include the root class ?
-
- On Stepstone and the POC, the header file to include is :
-
- <Object.h>
-
- On GNU cc and Apple cc, it's :
-
- <objc/Object.h>
-
- The root class is located in a directory called runtime for the
- Stepstone compiler, and in a directory called objcrt for the POC, but
- because of implicit -I options passed on to the preprocessor, these
- locations are automatically searched.
-
- 3.3 What is #import ?
-
- It's a C preprocessor construct to avoid multiple inclusions of the
- same file.
-
- #import <Object.h>
-
- is an alternative to
-
- #include <Object.h>
-
- where the .h file is protected itself against multiple inclusions :
-
- #ifndef _OBJECT_H_
- ...
- #define _OBJECT_H_
- #endif
-
- 3.4 Why am I lectured about using #import ?
-
- The GNU Objective-C compiler emits a warning when you use #import
- because some people find using #import poor style. You can turn off
- the warning by using the -Wno-import option, you could modify the
- compiler source code and set the variable warn_import (in the file
- cccp.c) or you could convert your code to use pairs of #ifndef and
- #endif, as shown above, which makes your code work with all compilers.
-
- 4. Object datatype (id)
-
- 4.1 What is id ?
-
- It's a generic C type that Objective-C uses for an arbitrary object.
- For example, a static function that takes one object as argument and
- returns an object, could be declared as :
-
- static id myfunction(id argument) { ... }
-
- 4.2 What is the difference between self and super ?
-
- self is a variable that refers to the object that received a message
- in a method implementation. super refers to the same variable, but
- directs the compiler to use a method implementation from the
- superclass.
-
- Using pseudo-code, where copy (from super) is the syntax for the copy
- implementation of the superclass, the following are equivalent :
-
- myObject = [super copy];
-
- and,
-
- myObject = [self copy (from super)]; // pseudo-code
-
- 4.3 What is @defs() ?
-
- It's a compiler directive to get access to the internal memory layout
- of instances of a particular class.
-
- typedef struct { @defs(MyClass) } *TMyClass;
-
- defines a C-type TMyClass with a memory layout that is the same as
- that of MyClass instances.
-
- 5. Message selectors (SEL)
-
- 5.1 What is a SEL ?
-
- It's the C type of a message selector; it's often defined as a
- (uniqued) string of characters (the name of the method, including
- colons), but not all compilers define the type as such.
-
- 5.2 What is perform: doing ?
-
- perform: is a message to send a message, identified by its message
- selector (SEL), to an object.
-
- 5.3 How do I know the SEL of a given method ?
-
- If the name of the method is known at compile time, use @selector :
-
- [myObject perform:@selector(close)];
-
- At runtime, you can lookup the selector by a runtime function that
- takes the name of the message as argument, as in :
-
- SEL mySel = selUid(name); // for Stepstone
- SEL mySel = sel_getUid(name); // for Apple
- SEL mySel = sel_get_any_uid(name); // for GNU Objective C
- SEL mySel = selUid(name); // for POC
-
- 6. Implementation pointers (IMP)
-
- 6.1 What is an IMP ?
-
- It's the C type of a method implementation pointer, a function pointer
- to the function that implements an Objective-C method. It is defined
- to return id and takes two hidden arguments, self and _cmd :
-
- typedef id (*IMP)(id self,SEL _cmd,...);
-
- 6.2 How do I get an IMP given a SEL ?
-
- This can be done by sending a methodFor: message :
-
- IMP myImp = [myObject methodFor:mySel];
-
- 6.3 How do I send a message given an IMP ?
-
- By dereferencing the function pointer. The following are all
- equivalent :
-
- [myObject myMessage];
-
- or
-
- IMP myImp = [myObject methodFor:@selector(myMessage)];
- myImp(myObject,@selector(myMessage));
-
- or
-
- [myObject perform:@selector(myMessage)];
-
- 6.4 How can I use IMP for methods returning double ?
-
- For methods that return a C type such as double instead of id, the IMP
- function pointer is casted from pointer to a function returning id to
- pointer to a function returning double :
-
- double aDouble = ((double (*) (id,SEL))myImp)(self,_cmd);
-
- 6.5 Can I use perform: for a message returning double ?
-
- No. The method perform: is for sending messages returning id without
- any other argument. Use perform:with: if the message returns id and
- takes one argument. Use methodFor: for the general case of any number
- of arguments and any return type.
-
- 7. Copying objects
-
- 7.1 What's the difference between copy and deepCopy ?
-
- copy is intented to make a bytecopy of the object, sharing pointers
- with the original, and can be overridden to copy additional memory.
- deepCopy is intented to make a copy that doesn't share pointers with
- the original. A deep copy of an object contains copies of its instance
- variables, while a plain copy is normally just a copy at the first
- level.
-
- 8. Objective-C and C++
-
- 8.1 How can I link a C++ library into an Objective-C program ?
-
- You have two options : either use the Apple compiler or use the POC.
- The former accepts a mix of C++ and Objective-C syntax (called
- Objective-C++), the latter compiles Objective-C into C and then
- compiles the intermediate code with a C++ compiler. See the compiler
- specific questions for more information.
-
- 9. Messages
-
- 9.1 How do I make a static method ?
-
- Methods are always implemented in Objective-C as static functions. The
- only way to obtain the IMP (implementation pointer) of a method is
- through the runtime (via methodFor: and friends), because the function
- itself is static to the file that implements the method.
-
- 9.2 How do I prevent an object from sending a given message ?
-
- You can't. If your object responds to a message, any other class can
- send this message. You could add an extra argument sender and check,
- as in :
-
- - mymethod:sender
- {
- if ([sender isKindOf:..]) ...
- }
-
- But this still requires cooperation of the sender, to use a correct
- argument :
-
- [anObject mymethod:self];
-
- 9.3 Do I have to recompile everything if I change the implementation of a
- method ?
-
- No, you only have to recompile the implementation of the method
- itself. Files that only send that particular messages do not have to
- be recompiled because Objective-C has dynamic binding.
-
- 10. Instance and Class Variables
-
- 10.1 Do I have to recompile everything if I change instance variables of a
- class ?
-
- You have to recompile that class, all of its subclasses, and those
- files that use @defs() or use direct access to the instance variables
- of that class. In short, using @defs() to access instance variables,
- or accessing instance variables through subclassing, breaks the
- encapsulation that the Objective-C runtime normally provides for all
- other files (the files that you do not have to recompile).
-
- 11. Objective-C and X-Windows
-
- 11.1 How do I include X Intrinsics headers into an Objective-C file ?
-
- To avoid a conflict between Objective-C's Object and the X11/Object,
- do the following :
-
- #include <Object.h>
- #define Object XtObject
- #include <X11/Intrinsic.h>
- #include <X11/IntrinsicP.h>
- #undef Object
-
- 12. Stepstone Specific Questions
-
- 12.1 How do I allocate an object on the stack ?
-
- To allocate an instance of 'MyClass' on the stack :
-
- MyClass aClass = [MyClass new];
-
- 13. GNU Objective-C Specific Questions
-
- 13.1 Why do I get a 'floating point exception' ?
-
- This used to happen on some platforms and is described at
- ftp://ftp.ics.ele.tue.nl/pub/users/tiggr/objc/README.387. A solution
- was to add -lieee to the command line, so that an invalid floating
- point operation in the runtime did not send a signal. DJGPP users can
- consult http://www.delorie.com/djgpp/v2faq/. AIX users may want to
- consult http://world.std.com/~gsk/oc-rs6000-problems.html. In some
- cases, you can fix the problem by upgrading to a more recent version
- of the GNU Objective-C runtime and/or compiler.
-
- 14. Apple Objective-C Specific Questions
-
- 14.1 What's the class of a constant string ?
-
- It's an NXConstantString.
-
- NXConstantString *myString = @"my string";
-
- 14.2 How can I link a C++ library into an Objective-C program ?
-
- c++ -c file.m
- c++ file.o -lcpluslib -o myprogram
-
- 15. Portable Object Compiler Objective-C Specific Questions
-
- 15.1 What's the syntax for class variables ?
-
- List the class variables after the instance variables, and group them
- together in the same way as instance variables, as follows :
-
- @implementation MyClass : Object { id ivar1; int ivar2; } : { id cvar1; }
- @end
-
- 15.2 How do I forward messages ?
-
- You have to implement doesNotUnderstand: to send a sentTo: message.
-
- - doesNotUnderstand:aMsg
- {
- return [aMsg sentTo:aProxy];
- }
-
- 15.3 How can I link a C++ library into an Objective-C program ?
-
- objc -c -cplus file.m
- objc -cplus file.o -lcpluslib -o myprogram
-
- 16. Books and further reading
-
- 16.1 Object-Oriented Programming : An Evolutionary Approach, 2nd Ed.
-
- Brad Cox & Andy Novobilski, ISBN 0201548348.
-
- 16.2 An Introduction To Object-Oriented Programming, 2nd Ed.
-
- Timothy Budd, ISBN 0201824191
-
- 16.3 Objective-C : Object-Oriented Programming Techniques
-
- Pinson, Lewis J. / Wiener, Richard S., ISBN 0201508281
-
- 16.4 Applications of Object-Oriented Programming; C++ SmallTalk Actor
- Objective-C Object PASCAL
-
- Pinson, Lewis J. / Wiener, Richard S., ISBN 0201503697
- _________________________________________________________________
-