views:

298

answers:

4

Hi,

can someone tell me why there is a * in the line?

What does it mean for the declaration?

NSString *someString;

Thank you guys

edit:

Thank you it helps me a lot but sometimes i see declarations without * example:

BOOL areTheyDifferent; why there is no pointer in this declaration?

+4  A: 

the * character means "pointer" in the C world (which Objective-C lives in)

someString is a pointer to an NSString object.

In Objective-C, you rarely need to worry about that fact, since all objects are passed around as pointers. You simply treat that someString variable as if it was an instance of the NSString class.

e.James
A: 

It tells the compiler that this is a pointer to a NSString.

Mr. Matt
A: 

'bool' is a built-in type in the compiler. For all primitive types that the compiler natively understands, making a pointer to such an object is not neccessary. For objective C classes, a pointer is always neccessary, due to the design of the language.

It's a bit hard to explain in a few lines...

Emiel
`bool` is built-in (sort of—at least under C99, it's a macro wrapping the built-in type `_Bool`), but `BOOL` is not: `BOOL`, `YES`, and `NO` are all macros defined by the Objective-C headers.
Peter Hosey
+2  A: 

You can't declare a variable as holding an object itself; you can only declare it as holding a pointer to an object. BOOLs aren't objects, so it's fine to have a variable containing a BOOL instead of a pointer to one. The same goes for numeric types, structures such as NSRange and NSRect, etc.—anything that isn't an instance of an Objective-C class.

I'm not sure why NeXT/Apple added this restriction. If I remember correctly, it only exists in Apple's version of GCC; the GNUstep version does not have it. That is, the GNUstep version allows you to declare a variable holding an object (NSString myString). Having never used GNUstep myself, I don't know how useful such variables really are in practice.

Peter Hosey
One reason not to support inline/sack objects might be to support retain and release. Retaining a stack object, or an object embedded in another object/structure doesn't really make sense since it can't live independently of its container.
Jon Hess