1

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?

1
  • 1
    Others have pointed out that * indicated the variable is a pointer to an NSString. As a next step, it might help to get a good understanding of the terms scalar, pointer, stack and heap as the apply to a language like C. Commented Jul 15, 2009 at 11:35

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

1 Comment

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

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

Comments

-1

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

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.