Where to declare global variable? in .h or.m?
Whats开发者_开发技巧 difference between declaring variable in .m file bellow @implementation and in .h fie in @interface??
@implementation PiechartViewController
NSMutableArray *impIDs;
I observed if i create 3 objects of piechartViewController new object overwrites previous object data i.e impIDs of 3 different object has same value that of last instantiated object.
instead if I followed this way
in ".h" file
@interface PiechartViewController : UIViewController {
NSMutableArray * impIDs;
Code works properly.Means impIDs has 3 different values.
First, class names start with uppercase letters and instance variables start with lower case letters. It is convention.
@implementation PiechartViewController
NSMutableArray *impIDs;
In the above, impIDs
is a variable that is defined within the scope of the file containing that code. While it is a global, more or less, you won't be able to access it from other files without declaring it somewhere that is visible to them (something like extern NSMutableArray *impIDs
).
@interface PiechartViewController : UIViewController {
NSMutableArray * impIDs;
Here, impIDs
is an instance variable and, thus, each instance will have storage for it's own isolated bit of data accessible in the impIDs
instance variable slot.
I'd suggest you read this.
精彩评论