“可变未声明”编译到iOS设备时出错,而不是模拟器 [英] "Variable Undeclared" error when compiling to iOS Device, but not for Simulator
问题描述
我有一个自定义UIVIewController,它是其他控制器的基类,并有一个自定义UIView变量的实例,可通过继承这些类访问。
BaseViewController。 h
@interface BaseViewController:UIViewController {
UIView * _vwHeader;
}
@end
BaseViewController.m
#importBaseViewController.h
@implementation BaseViewController
- (void)loadView {
[super loadView];
_vwHeader = [[UIView alloc] init];
}
@end
CustomViewController.h
#importBaseViewController.h
@interface CustomViewController:BaseViewController
@end
CustomViewController.m
#importCustomViewController.h
@implementation CustomViewController
- (void)loadView
{
[super loadView];
[_vwHeader setHidden:NO];
}
@end
问题是,当我我在模拟器上运行它的一切工作完美,但是当我改变到设备我有一个错误在 [_ vwHeader setHidden:NO];
行说:'_ vwHeader'undeclared(在此函数中第一次使用)
我已经尝试过:
- 注释这行代码,但是它给我一个错误在另一个类使用基类的变量,同样的方式(它只返回一个错误),所以看起来它不是视图或控制器类中的具体错误,因为错误发生在具有不同类型的其他clases中,例如
UIView
和NSObject
types - 更改目标编译器配置,如:architectures(所有),base sdk
似乎可以解决问题,但不完全
- 为
_vwHeader
创建属性并通过self._vwHeader
或super._vwHeader
似乎工作,但是必须创建一个属性只是为了访问一个变量不让我confortable,特别是因为我必须为我的项目中的同一种情况下的所有变量。 - 更改了C / C ++编译器版本:使用
Apple LLVM Compiler 2.1
使编译错误消失,与项目中使用的其他库有关的其他问题。因此,它不是一个确定的解决方案,但可能是一个线索的问题是什么。
/ p>
我试图创建另一个不是指针的变量, BOOL
而不是 UIView *
,然后在继承的类中使用它:
EDIT(2):
我没有任何属性在任何我的类,我仍然得到的错误。
我只是添加了测试porpouses的属性,看看父类中的属性是否导致相同的行为,显然它不是。
这也是奇怪的是,当我得到错误的变量,我检查了我的intellisense,它找到... ...
为了引用 self
以外的任何对象中的实例变量,包括 super
,必须使用结构指针运算符( - >
)。实例变量的默认范围是protected,这意味着它只能在它定义的类中或该类的子类中访问。因为 CustomViewController
是 BaseViewController
的子类,所以这个范围足以使用 self - > _vwHeader
,但如果你试图从这里做的第二个类不是一个子类,你还需要改变范围为 @public
或 @package
。
总之,将您的方法调用更改为:
[self-> _vwHeader setHidden:NO];
,它应该适用于基本视图控制器的任何子类。
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
andNSObject
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 byself._vwHeader
orsuper._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...
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.
这篇关于“可变未声明”编译到iOS设备时出错,而不是模拟器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!