我不是白痴,但是头文件让我有时感觉像个人。我有一个可能是一个过于复杂的设置,有一个我无法解决的错误。这里的细节就像我能做到的那样简单......
当创建一个Layer并将Controller的模型传递给Layer的模型(在AScene.m
中)时,会出现问题。我得到的“请求成员'模型'不是结构或联合”。投射不起作用,我不知道如何让这些课程互相交谈。我认为问题的一部分可能是Controller类包含Model类。
或者Controller.h
#import <Foundation/Foundation.h>
@class Model;
@class AScene;
@interface Controller : NSObject {
Model *Model;
}
@property (nonatomic, retain) Model *Model;
-(void)runScene;
Controller.m或者
#import "Controller.h"
#import "Model.h"
#import "AScene.h"
@implementation Controller
@synthesize Model;
- (void)runScene {
AScene *newScene = [[AScene alloc] init];
newScene.controller = self;
}
RootScene.h
#import "cocos2d.h"
@class Controller;
@interface RootScene : Scene {
Controller *controller;
}
@property (nonatomic, retain) Controller *controller;
@end
RootScene.m
#import "RootScene.h"
#import "Controller.h"
@implementation RootScene
@synthesize controller;
- (id) init {
self = [super init];
if (self != nil) {
//
}
return self;
}
- (void) dealloc {
[controller release];
[super dealloc];
}
@end
AScene.h
#import "RootScene.h"
@class ALayer;
@class Model;
@interface AScene : RootScene {
}
@end
AScene.m
#import "AScene.h"
#import "ALayer.h"
#import "Model.h"
@implementation AScene
- (id) init {
self = [super init];
if (self != nil) {
ALayer *newLayer = [ALayer node];
newLayer.model = controller.Model; // <-- Request for member 'Model' in something not a stucture or union
[self addChild:statusScreenLayer];
}
return self;
}
- (void) dealloc {
[super dealloc];
}
@end
RootLayer.h
#import "cocos2d.h"
@class Model;
@interface RootLayer : Layer {
Model *model;
}
@property (nonatomic, retain) Model *model;
@end
RootLayer.m
#import "RootLayer.h"
#import "Model.h"
@implementation RootLayer
@synthesize model;
- (id) init {
self = [super init];
if (self != nil) {
//
}
return self;
}
- (void) dealloc {
[model release];
[super dealloc];
}
@end
ALayer.h
#import "RootLayer.h"
@interface ALayer : RootLayer {
}
-(void) draw;
@end
ALayer.m
#import "ALayer.h"
@implementation ALayer
-(void) draw {
// draw based on state of the model
}
@end
答案 0 :(得分:3)
AScene
的实施不会#import
Controller
的标题。
编辑:明确的解决方案。
在AScene.m中添加:
#import "Controller.h"
答案 1 :(得分:1)
尼古拉似乎是对的。除此之外,最佳实践将规定属性(如ivars)不应以大写字母开头。你只是要求受伤。请改用:
<强>或者Controller.h 强>
@interface Controller : NSObject {
Model *model;
}
@property (nonatomic, retain) Model *model;
<强> Controller.m或者强>
@synthesize model;