objective c xcode 4.0.2:子类无法访问超类变量“未在此范围内声明” [英] objective c xcode 4.0.2: subclass can't access superclass variables "was not declared in this scope"
问题描述
我有几个类是一个Layer类的子类..由于某种原因,其中一个子类的行为不同。这是一个精简版:
I have several classes that are subclasses of one Layer class.. for some reason one of the subclasses acts differently. this is a stripped down version:
@interface Layer: CCLayer
{
GameScene* _scene;
CCNode* _layerNode;
}
@end
#import "Layer.h"
@interface UILayer: Layer
{
}
@end
@implementation UILayer
-(void) doStuff
{
[_layerNode addChild:[CCNode node]]; <---gives compile error: "_layerNode was not declared in this scope"
[_scene playSound];<------gives compile error: "_scene was not declared in this scope"
}
@end
我认为这是基本的想法。我可以通过这样做来解决这个问题
I think that gets the basic idea across. I can fix this by doing
[[_self _layerNode] addChild:[CCNode node]];
[[_self _scene] playSound];
但是我无法弄清楚为什么Layer的其他子类可以直接访问_layerNode和_scene?我猜这是构建设置的问题。此外,只有当我为设备(iphone)而不是模拟器构建它时,才会出现此问题。如果您需要更多信息,请与我们联系。
but what I can't figure out is why other subclasses of Layer can access _layerNode and _scene directly? I am guessing that it is a problem with the build settings. Further this problem only happens when I build it for the device (iphone) and not the simulator. let me know if you need more information to answer.
编辑
哎呀我写错了。它应该是 [[self _layerNode] addNode:[CCNode node]]
但我猜这个问题是为什么一个子类可以直接访问ivar _layerNode
,另一个必须使用 [self _layerNode]
访问它。就像UILayer找不到超类头一样
Oops I wrote it wrong. It should have been [[self _layerNode] addNode:[CCNode node]]
but I guess the question is why would one subclass have direct access to the ivar _layerNode
and another have to access it with [self _layerNode]
. It is like the UILayer can't find the super class header
推荐答案
来自文档: Apple Objective-C编程语言 (不提这个不再了,但你可以在这个已退休的文件中找到它)。
From the docs: Apple Objective-C Programming Language (doesn't mention this anymore, but you can find it in this retired document).
实例变量可以在声明它的类中以及继承它的类中访问。没有显式范围指令的所有实例变量都具有
@protected
范围。
但是,可以访问公共实例变量在任何地方,好像它是C结构中的一个字段。例如:
However, a public instance variable can be accessed anywhere as if it were a field in a C structure. For example:
Worker * ceo = [[Worker alloc] init];
ceo-> boss = nil;
我使用 LLVM GCC 4.2
编译错误(对于iOS项目,在设备上):
I have the compilation error using LLVM GCC 4.2
(for an iOS project, on device):
错误:'fooInstanceVar'未声明(在此函数中首次使用)
和使用GCC 4.2的同一个:
错误:'fooInstanceVar'未声明(首次在此函数中使用)
error: 'fooInstanceVar' undeclared (first use in this function) and the same one using GCC 4.2 : error: 'fooInstanceVar' undeclared (first use in this function)
我可以使用 LLVM编译器2.0
编译而不会出错。
I can compile using LLVM Compiler 2.0
whithout error.
使用<$编译 LLVM GCC 4.2
和 GCC 4.2
c $ c> self-> :
For compiling with LLVM GCC 4.2
and GCC 4.2
with the use of self->
:
[NSArray arrayWithObjects:self。 barProperty,self-> fooInstanceVar,nil];
doSomethingWithProperty
方法中的
。
[NSArray arrayWithObjects:self.barProperty, self->fooInstanceVar, nil];
in thedoSomethingWithProperty
method.
这篇关于objective c xcode 4.0.2:子类无法访问超类变量“未在此范围内声明”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!