Reputation: 18149
In objective-c, I can declare an int
or bool
etc in the .m
file, outside any function. That allows me to use such variable everywhere in the class.
I can also declare such variables in the .h
file, inside the interface block, achieving the same result.
Well, my question is: what is the difference? Is there? Or is it all a matter of organization?
Upvotes: 0
Views: 126
Reputation: 34625
In second case, it is a global variable that has external linkage. Meaning, it can be accessed other translation units / source files using extern
keyword. But in first case, it is a part of interface, so it can be only used by it's member functions and any other interfaces derived from this interface depending on access specifier.
Upvotes: 3
Reputation: 24846
In first case they become gloabal variables in .m file and are are shared between all the instances of the interface. In second case the will be separate for multiple instances. The common way is to declare interface variables with in the interface
Upvotes: 2