Saturn
Saturn

Reputation: 18149

Declaring variables within interface or implementation?

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

Answers (2)

Mahesh
Mahesh

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

Andrew
Andrew

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

Related Questions