“static” keyword for global variables - What's the scope limited to?
-
19-04-2021 - |
Question
Prefixing a variable declared outside of any scope with the keyword static prevents that variable from being externally accessible. However, does it limit the scope from a category using it?
Foo.m
@implementation Foo
static void* FooContext = &FooContext;
- (void)methodThatUsesFooContext { ... }
@end
Foo+SpecialSauce.h
@implementation Foo (Special Sauce)
- (void)anotherMethodThatWouldLikeToUseFooContext { ... }
@end
Solution
Static variables declared at the top-level of a file (i.e., outside of any functions or method calls) are visible to anything within that file.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow