OemerA
OemerA

Reputation: 2672

Cocoa variable declaration

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?

Upvotes: 1

Views: 804

Answers (4)

Peter Hosey
Peter Hosey

Reputation: 96323

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.

Upvotes: 2

Emiel
Emiel

Reputation: 1502

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

Upvotes: -1

Codebeef
Codebeef

Reputation: 43996

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

Upvotes: 0

James Eichele
James Eichele

Reputation: 119144

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.

Upvotes: 4

Related Questions