3

I'm working through a code listing from a book and it has a pair of variables (specifically NSString *) declared and initialised in the @implementation rather than the @interface but outside of any method body. I've not seen this before and I'm wondering what the difference this makes in scope and so on.

I've had a quick look in The Objective C Programming Language but I can't see anything describing what effect this has.

Thanks

Andy

1
  • will you post as code sample that shows what you're talking about? Commented Jun 17, 2010 at 12:42

1 Answer 1

8

Variables declared inside @implementation have global scope.

If you declare them as "static", they are only visible from the methods in the same source file.

So:

@implementation MyClass

NSString *myString; // global scope, and accessible by all code in your project

or

@implementation MyClass

static NSString *myString; // global scope, but only accessible by code 
                           // in this source file
Sign up to request clarification or add additional context in comments.

3 Comments

I was searching for answer and realized that if its in bracelet, then it's actually ivar. See stackoverflow.com/questions/6785765/…
are you sure about that ?? I can't make it work. Perhaps you made wrong comments
What's the difference between defining them outside the @implementation block or inside it?

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.