Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C Modifers for static?

How do I create a static variable in my Objective-C class? I'm familiar with using @private in my header files for private variables, but I am trying to create a static method which accesses a static variable. How should I declare this static variable in my header file?

like image 324
StanLe Avatar asked Feb 21 '26 06:02

StanLe


2 Answers

Objective-C simply follows C in this regard - you make static file variables. In your implementation (ie your .m file) put a declaration anywhere (but ideally somewhere sensible like at the top of the file, or even in the relevant method if it's only accessed in one place).

If you want to provide controlled access to such a static, put it outside of any method implementation, and use class methods to access it.

like image 141
Bored Astronaut Avatar answered Feb 22 '26 20:02

Bored Astronaut


Static variables for Objective-C follow the same rules for static variables in C (storage modifier). You can declare your static variables at file or function scope but they have no relation to your class like instance variables do.

like image 22
Joe Avatar answered Feb 22 '26 19:02

Joe



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!