Felipe Sabino Felipe Sabino - 2 months ago 7
iOS Question

"Variable Undeclared" error when compiling to iOS Device, but not for Simulator

I have an custom UIVIewController that is the base class for other controllers and has an instance of a custom UIView variable that is accessed by inherited the classes.

BaseViewController.h

@interface BaseViewController : UIViewController {
UIView *_vwHeader;
}

@end


BaseViewController.m

#import "BaseViewController.h"
@implementation BaseViewController

-(void)loadView {

[super loadView];

_vwHeader = [[UIView alloc] init];
}

@end


CustomViewController.h

#import "BaseViewController.h"
@interface CustomViewController : BaseViewController

@end


CustomViewController.m

#import "CustomViewController.h"
@implementation CustomViewController

- (void)loadView
{
[super loadView];

[_vwHeader setHidden:NO];
}

@end


The problem is that when I am running it on the simulator everything works perfectly fine, but when I change to the device I have an error on the
[_vwHeader setHidden:NO];
line which says:
'_vwHeader' undeclared (first use in this function)


I already tried to do:


  • Comment this line of code, but then it gives me an error in another class using a variable from the base class the same way (It only returns one error at a time), so it seems that it is not an specific error in the view or the controller class as the error occurs in other clases with different types, such as
    UIView
    and
    NSObject
    types

  • Change target compiler configuration, such as: architectures (all of them), base sdk (all above 4.0) didn't change anything



What seem to solve the problem, but not completely


  • Creating a property for
    _vwHeader
    and accessing it by
    self._vwHeader
    or
    super._vwHeader
    seems to work, but having to create a property just to access a variable does not make me confortable, specially because I would have to do it for all variables in the same situation inside my project.

  • changed C/C++ compiler version: using
    Apple LLVM Compiler 2.1
    makes the compilation error goes away, but gives a bunch of other problems with other libraries being used in the project. So, it is not a definitive solution, but might be a clue of what the problem is.



EDIT:

I tried to create another variable that is not a pointer, a
BOOL
instead of the
UIView *
and then used it in the inherited class: the problem also occurs

EDIT (2):

I have no properties whatsoever in any of my classes and I still get the error.
I just added the properties for test porpouses, to see if a property in a parent class caused the same behaviour, and apparently it doesn't.
Something that is also weird is that when I get the error in the variable, I checked with my intellisense and it finds it...

Answer

In order to refer to an instance variable within any object other than self, including super, you must use the structure pointer operator (->). The default scope of an instance variable is protected, which means it can only be accessed within the class it is defined in or a subclass of that class. Since CustomViewController is a subclass of BaseViewController, this scope is sufficient to access the variable using self->_vwHeader, but if the second class you were trying to do this from is not a subclass you will also need to change the scope to either @public or @package.

In summary, change your method call to:

[self->_vwHeader setHidden:NO];

and it should work for any subclasses of the base view controller.