Andrew
Andrew

Reputation: 698

variables declared in @implementation

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

Upvotes: 3

Views: 1315

Answers (1)

Philippe Leybaert
Philippe Leybaert

Reputation: 171854

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

Upvotes: 8

Related Questions