objective c xcode 4.0.2: subclass can't access superclass variables “was not declared in this scope”

落爺英雄遲暮 提交于 2019-12-23 03:09:07

问题


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];

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.

Edit:

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


回答1:


From the docs: Apple Objective-C Programming Language (doesn't mention this anymore, but you can find it in this retired document).

The instance variable is accessible within the class that declares it and within classes that inherit it. All instance variables without an explicit scope directive have @protected scope.

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;

I have the compilation error using LLVM GCC 4.2 (for an iOS project, on device):

error: 'fooInstanceVar' undeclared (first use in this function) and the same one using GCC 4.2 : error: 'fooInstanceVar' undeclared (first use in this function)

I can compile using LLVM Compiler 2.0 whithout error.

For compiling with LLVM GCC 4.2 and GCC 4.2 with the use of self->:

[NSArray arrayWithObjects:self.barProperty, self->fooInstanceVar, nil]; in the doSomethingWithProperty method.




回答2:


Maybe it's the name. The 'UI' prefix is designated for UIKit classes. Could well be that UILayer is a private UIKit class used by Apple.




回答3:


If you @synthesize ivars, the compiler unhelpfully stops you from accessing them "directly". The fix is relatively easy: Write self->_layerNode and self->_scene.

I'm assuming your "stripped-down version" compiles fine.

Note that it's also bad form to directly access your superlcass's ivars unless you're aware of its implementation details.

(What's _self?)



来源:https://stackoverflow.com/questions/6394989/objective-c-xcode-4-0-2-subclass-cant-access-superclass-variables-was-not-dec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!