Objectmix
Tags Register Mark Forums Read

Objective-C FAQ : C

This is a discussion on Objective-C FAQ within the C forums in Programming Languages category; Archive-name: computer-lang/Objective-C/faq Posting-Frequency: monthly Frequently Asked Questions - comp.lang.objective-c compiled by David Stes (stes @ pandora.be) September 14 2006 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 ...


Object Mix > Programming Languages > C > Objective-C FAQ

C comp.lang.c usenet archive

Reply

 

LinkBack Thread Tools
  #1  
Old 09-14-2006, 12:52 PM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Objective-C FAQ

Archive-name: computer-lang/Objective-C/faq
Posting-Frequency: monthly



Frequently Asked Questions - comp.lang.objective-c

compiled by David Stes (stes@pandora.be)

September 14 2006

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...bjective-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

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/t...bjc/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
_________________________________________________________________
Reply

Thread Tools


Similar Threads

Thread Thread Starter Forum Replies Last Post
Objective-C FAQ usenet C 0 10-15-2006 12:51 PM
Objective-C FAQ usenet C 0 01-03-2005 01:52 PM
Objective-C FAQ usenet C 0 11-24-2004 01:54 PM
Objective-C FAQ usenet C 0 06-18-2004 12:58 PM
Objective-C FAQ usenet C 0 01-14-2004 01:52 PM


All times are GMT -5. The time now is 08:33 AM.

Managed by Infnx Pvt Ltd.