Reputation: 1747
I am puzzled by some behaviour I'm seeing in Objective-C.
I have a method as follows:
-(void)showFormWithId:(NSString*)formId andMode:(int)mode
{
HPSModelForm* model = [HPSDbUtilities getForm:formId];
HPSFormController* formVC = [ [ HPSFormController alloc ] init ];
[(UINavigationController*) self.view.window.rootViewController pushViewController:formVC animated:YES];
}
Within the HPSFormController class my implementation contains the following:
@implementation HPSFormController
NSArray* _arrayOfPageNosWithSummaryElements;
i.e. _arrayOfPageNosWithSummaryElements is not a property, but is an ivar visible to any method within the HPSFormController class. It is not declared in the header file at all.
When I call showFormWithId the first time then _arrayOfPageNosWithSummaryElements is nil. However, when I call showFormWithId a second time then it seems to me that _arrayOfPageNosWithSummaryElements is not nil, but has the value from the previous instance of HPSFormController. I don't understand this - surely because the scope of the formVC is the showFormWithId method then the second time I call showFormWithId then a completely new instance of HPSFormController should be created with _arrayOfPageNosWithSummaryElements uninitialised and therefore set to nil?
What am I doing wrong? Thanks.
Upvotes: 0
Views: 60
Reputation: 4406
@implementation HPSFormController
NSArray* _arrayOfPageNosWithSummaryElements;
is no ivar declaration but a declaration of a global variable. You have to use brackets:
@implementation HPSFormController {
NSArray* _arrayOfPageNosWithSummaryElements;
}
Upvotes: 0
Reputation: 49376
It's not actually in instance variable here at all (instance variables are declared in the interface
section of a class). You're declaring a global variable, in the normal C sense.
Upvotes: 3