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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With