Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

variables declared in @implementation

Tags:

objective-c

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

like image 608
Andrew Avatar asked Jun 04 '26 10:06

Andrew


1 Answers

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
like image 135
Philippe Leybaert Avatar answered Jun 06 '26 01:06

Philippe Leybaert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!